Question

For this interactive assignment, you will continue to utilize loops and functions and write a Python...

For this interactive assignment, you will continue to utilize loops and functions and write a Python program that asks the user for an integer (number) greater than 1.

Once the user enters an integer, it should then allow the user to CHOOSE between the following two options:

If 1 is entered, a countdown from that number to zero is printed.

If 2 is entered, the factorial of the number is printed.

If the user inputs a number less than 1, your program should display a message informing the user that the program only accepts numbers greater than 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#fucntion to print the countdown of the number n
def countdown(n):
print('Countdown: ')
#a loop is run for 'n' times
for i in range(n):
#the number is printed in reverse order
print(str(n-i))
#lastly the number 0 is printed
print('0')

#function to calculte factorial of the passed number n
def fact(n):
#variable that stores the factorial is intialised by 1
fact = 1
#a loop is run for 'number' times
for i in range(number):
#factorial is calculated
fact *= i+1
#then it is printed
print('The factorial of ',str(number),'is: ',str(fact))
  
#the variable which stores the entered number
number = 0
#while loop which gets terminated only if the user enters a number greater than 1
while(number<1):
#Asking the user for an input here
number = int(input('Enter a number greater than 1: '))
#giving the user two choices
print('Enter 1 to get a countdown from ',str(number),' to zero or press 2 to get the factorial of that number.')
choice = int(input())

#if the number entered was 1 by the user then the code will print a countdown
if choice == 1:
#calling the countdown function
countdown(number)
#if the user enters number 2 as choice
elif choice == 2:
#calling the function fact
fact(number)
#if the user enters something other than 1 or 2 then the following is printed
else :
print('Invalid Option selected!')

CODE IMAGES:

OUTPUT:

feel free to ask any questions in the comments section to get your doubts cleared
Thank You!

Add a comment
Know the answer?
Add Answer to:
For this interactive assignment, you will continue to utilize loops and functions and write a Python...
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
  • Using loops, write a C# program that asks the user to enter repeatedly an integer number,...

    Using loops, write a C# program that asks the user to enter repeatedly an integer number, it stops when the user enters -1, then the program should display how many numbers were entered, their the sum and their average

  • Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the...

    Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the factorial function. Since methods have not yet been covered in detail, you may do the calculations in a class that contains just a main method. The looping structures require the use of count-controlled loops. This program computes the factorial function of a non-negative integer, which is written mathematically as n! (the parameter n followed by an exclamation mark). For values of n equal to...

  • Use Python and only while loops For this part of the homework you will write code...

    Use Python and only while loops For this part of the homework you will write code to draw an isosceles right triangle. (A right triangle in which the height and base are the same size.) Your program should prompt the user for these inputs, in exactly this order: 1. The height of their triangle 2. The symbol the triangle will be outlined in 3. The symbol the triangle will be filled with For these inputs, you can assume the following:...

  • PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI()...

    PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI() This function prints out your name and lists any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and prints out a message indicating if the number is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • ((python programming)) 2. In this exercise you will create a program that reads a letter of...

    ((python programming)) 2. In this exercise you will create a program that reads a letter of the alphabet from the user. If the user enters a, e, i, o or u then your program should display a message indicating that the entered letter is a vowel. If the user enters y then your program should display a message indicating that sometimes y is a vowel and sometimes y is a consonant. Otherwise your program should display a message indicating that...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Write a Python program that asks the user to enter the number of calories and fat...

    Write a Python program that asks the user to enter the number of calories and fat grams in a food item. The program should display the percentage of the calories that come from fat. One gram of fat has 9 calories, therefore: Calories from fat = fat grams * 9 The percentage of calories from fat can be calculated as follows: Calories from fat / total calories If the calories from fat are less than 30 percent of the total...

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