Home > Blockchain >  Asking for user input in print function - Python 2.7
Asking for user input in print function - Python 2.7

Time:07-13

Sorry if this seems like a bit of a dumb question but I’m a bit confused about a Python 2 exercise I am trying to complete.

The exercise asks to write a code that will find the factorial of a number.

I have written a function that does this but I am confused about the pre set print function that has asks for user input as I’ve only seen this done as a separate thing. Could anyone help me figure out how to make this work please as the exercise asks for the print function to not be deleted.

Thank you in advance!

def FirstFactorial(num):

  # code goes here
  fact = 1
  while num > 0:
    fact *= num
    num -= 1
  return fact

x = raw_input("enter a number: ")
result = FirstFactorial(x)
# keep this function call here
print FirstFactorial(raw_input())

CodePudding user response:

You have to print result not print FirstFactorial(raw_input())

The way it is set up now the program will work for the first input, but it will never print it out so you never see result.

Then it will look for a second input from your second call to raw_input(), but the user will not be notified that the program wants a second input. The second output of FirstFactorial() will be correctly printed.

  • Related