while num > 1:
factorial = factorial * num
num = num - 1
Answer from John Gordon on Stack OverflowVideos
Factorial if you don't know is for ex. Say 5!=54321=120 I wrote this code:
n=1 k=input("Enter factorial here: ")
While n>=k:
print(n) n=n+1
........................ Output comes say if k=3
3
2
1
I want to know How can I get 321 using while loop.
So I took a beginner Python class and I'm already lost.
We are supposed to build a calculator using for/while -loop. The program is supposed to ask a number and if the choice is "0" - the program will close. If the input is "1", the program will ask another number and will then get the factorial value of that number.
I can barely understand what I'm supposed to be doing and I got my code working as long as the input value is 0-3. After that it doesn't work. This is the given example:
0! = 1
1! = 0!*1 = 1
2! = 1!*2 = 2
3! = 2!*3 = 6
4! = 3!*4 = 24
5! = 4!*5 = 120
6! = 5!*6 = 720
Edit:
Did all the changes and the code looks like this now! Still not passable tho. D:
Here are the requirements in case I have misunderstood something:
"You need to have variables "factorial" and " counter" they both have int values of 1!"
"While -loop must repeat until counter is less than or equal to num"
"You need to assign a variable called "factorial2" from factorial * counter"
"counter value +1 after every loop"
choice = input('0 - closes the program, 1 - get factorial value: ')
while True:
if choice == '1':
num = input('Give a number: ')
num = int(num)
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
print(str(num) + '! = ', factorial)
elif choice == '0':
print('Program will be closed!')
break
else:
print('Please give either 0 or 1!')
choice = input('0 - closes the program, 1 - get factorial value: ')
continueI'm so lost. This is literally my third assignment and all our materials for this assignment teach us is the basic functions of while/for -loop. :( Feeling so disheartened at the moment.
EDIT: Made some progress. It's still not passable but it's closer. I got a tip that I need to assign factorial2 variable from factorial * counter But... I don't get how can I do this. This code does what is asked, but it's not done the right way I guess...