Question

PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

PYTHON 3 - please show format

Question 2. )

Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input.

For example:

Test Input Result
print(yesOrNo()) hello
blank
no
False

Question 3.)

Write a program that lets the user input integers one at a time at the keyboard until they type the worddone on a line by itself. Print the sum of the values entered. You may assume the user will only enter integers or "done".

For example:

Input Result
3
5
done
8

Question 4.)

Assume that you have a function called is_prime which consumes a positive number and returns True if the number is prime and False if the number is not prime.

Your task is to now to use the Design Recipe to write a main function, which prompts the user for a positive integer n and prints the first n primes to the console.

If the user input 3, your program should display:

2

3

5

Question 5.)

A variable num has already been defined. Write a while loop which calculates the sum of the perfect squares less than or equal to num. Store the result in the variable acc.

For example, if num is 10, then acc should be 1+4+9=14.

For example:

Question 6.)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:

Fn = Fn-1 + Fn-2

F1 = 1

F0 = 1

The first 12 Fibonacci numbers follow:

1 , 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.

Use the Design Recipe to write a function called fib which consumes n and returns the nth Fibonacci number. Use accumulation variables to implement your fib function. Hint: you will need three accumulation variables to keep track of Fn and Fn-1 and Fn-2.

You should create special cases for n of 0 or 1 and then use a for loop over the range 2 through n to compute the nth term.

For example:

Test Result
print(fib(10)) 89
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1 def yesOrNo(): 3while st yes and stno: 4 st input () 5if styes 6 return True return False 8 9 print(yesorNo)) 10 12 S

def yesOrNo():
st = ""
  
# checking it the string is yes or no. If it is neither of them, it asks for input
# else it returns True for yes and False for no
while st != "yes" and st != "no":
st = input()
if st == "yes":
return True
return False

print(yesOrNo())

'''
SAMPEL OUTPUT
hello
blank
no
False
'''

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.......

    In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.... In mathematical notation the sequence Fn of Fibonacci number is defined by the following recurrence relation: Fn=Fn-1+Fn-2 With the initial values of F0=0 and F1=1. Thus, the next number in the series is the sum of the previous two numbers. Write a program that asks the user for a positive integer N and generate the Nth Fibonacci number. Your main function should handle user...

  • Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that...

    Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that consumes a list of numbers and prints a histogram graph using asterisks to represent each number in the list. Use one output line per number in the list. You may assume that only integers are passed to this function. Your function should ignore negative values. Include a docstring! Test Result print_histogram([ 0, 2, 4, 1]) ** **** * print_histogram([10, 5, 3, -1, 8]) **********...

  • Python Use the Design Recipe to write a function called running_average that repeatedly asks the user...

    Python Use the Design Recipe to write a function called running_average that repeatedly asks the user to input integers at the keyboard until they type the word done. Return the average of the values they entered. You may assume the user will only enter integers or "done". Include a docstring!

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • Python Language The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe...

    Python Language The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe to write a function called blip_blop_day that consumes an int representing the number of days that have passed since their celebration start day and returns name of the current day. For example, if 2 days have passed, then 'Thursday' should be returned. Include a docstring! For example: Test Result 2 Thursday 4 Saturday Monday Tuesday 6 7 8 Wednesday

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...

    Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above. firstPrime: is 2 secondPrime: the first prime number after 2 thirdPrime: the third prime number …. nth prime: the nth prime number Your program must be organized as follows: int main() { //prompt the user to enter n //read n from the...

  • This is Python coding question, and topic is recursive. Can anyone help me figuring this out?...

    This is Python coding question, and topic is recursive. Can anyone help me figuring this out? (Only recursive function is allowed.) Thanks. Write a function called Fibonacci(n) that takes as a parameter an integer n and returns the nth Fibonacci number. The first two Fibonacci numbers are 1 and every subsequent one is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, .... Write a recursive function to count the number of uppercase letters in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT