Question

In: Computer Science

Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of...

Python 3

Calculate factorial

Create a function that uses a loop to calculate the factorial of a number.

* function name: get_fact
* parameters: num (int)
* returns: int
* operation:
Must use a loop here. Essentially calculate and return the factorial of
whatever number is provided.
but:
- If num is less than 1 or greater than 20, print "num is out of range"
and exit the function
- If num is not an int, print "invalid num parameter" and exit (easy way
to do this is to use exception handling around your range() function if you
use a for loop)
Recall that factorial of n is the product of all integers from 1 to n. For
example the factorial of 5 (5!) is 1*2*3*4*5 which is equal to 120. 4! is 24.
6! is 720. This will be extremely similar to the adding maching problem from
last time EXCEPT instead of adding a user-entered number each time, you are
instead multiplying by the next number in a sequence.
E.g.,
INITIALIZE PROD VARIABLE
LOOP X FROM 1 TO NUM
PROD GETS THE VALUE OF PROD TIMES X
RETURN PROD

Though of course you'll need a little more than that logic there to handle
the input validation.
* expected output:

>>> get_fact(5)
# RETURNS 120
>>> get_fact(6)
# RETURNS 720
>>> get_fact(21)
num is out of range
# RETURNS None
>>> get_fact('dude')
invalid num parameter
# RETURNS None
>>> get_fact(7.1)
invalid num parameter
# RETURNS None


Factorial of what: 6
6! is 720
Factorial of what: 25
num is out of range
Factorial of what: 0
Goodbye!

Solutions

Expert Solution

Code:

def get_int(num):
    if(num<1 or num>20):
        print("num is out of range")
        return None
    else:
        PROD=1;
        for x in range(1,num):
            PROD=PROD*x;
        return PROD
a=int(input("Factorial of what:"))
j=get_int(a);
if(j!=None):
    print(a,"! is ",j);


Output:


Related Solutions

2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
Homework 3 Loop and Function (PYTHON) You are asked to develop a compensation calculator application for...
Homework 3 Loop and Function (PYTHON) You are asked to develop a compensation calculator application for a department store. At the department store, the compensation of sales staff consists of a base salary and a commission. The base salary is $5,000, and the commission rate is tiered based on sales amount as following: Sales AmountCommission Rate $0.01 – $5,000 8% $5,000.01 – $10,000 10% $10,000.01 and above12% For example, if the sales amount is $12,000, the commission is calculated as...
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
In C++. Write a program that uses array to calculate the factorial of a reasonable large...
In C++. Write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).  
Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT