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
How do i create a python function that will calculate the Levenshtein distance given a user...
How do i create a python function that will calculate the Levenshtein distance given a user input of two words? I am aware that there are packages already made to do this, but I want to create my own. Please explain all the steps. https://en.wikipedia.org/wiki/Levenshtein_distance#:~:text=Informally%2C%20the%20Levenshtein%20distance%20between,considered%20this%20distance%20in%201965.
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...
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).  
Microsoft Visual C++ Assembly language Problem 3. Write a program that uses a loop to calculate...
Microsoft Visual C++ Assembly language Problem 3. Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(1) = 1, Fib(2) = 2, … Fib(n) = Fib(n-1) + Fib(n-2). Place each value in the EAX register and display it with a call DumpRegs statement inside the loop For example: Fib(1) = 1, Fib(2) = 2, Fib(3) = Fib(1) + Fib(2) = 3, Fib(4) = Fib(2) + Fib(3)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT