0

I wrote a program to calculate the factorial of a number all work perfectly but crashes when for test I enter a float number. My goal is for a floating number to be accepted but not calculated. As the program will accept but return something like "Bad entry, only integers are accepted." I have tried multiple statement but it only work for the number I put in the statement. So I thought maybe something should be built, maybe by naming some floats and doing some sort of subtraction. But I get lost. here is the program I have so far without the floating statement included:

    def main():
# take input from the user
        num = int(input("Enter a number: "))
        factorial = 1
        if num > 100:
            print("Bad entry. It should be an integer less than or equal to 100!")
            print("Please try again: ")
        elif num == 0:
            print("The factorial of 0 is 1")
        elif num < 0:
            print("Bad entry. It should be an integer superior than or equal to 0!")
            print("Please try again: ")  
        else:
            for i in range(1,num + 1):
                factorial = factorial*i
            print("The factorial of",num,"is",factorial)

main()
Miteux
  • 3
  • 1
  • 3

3 Answers3

3

You should use a try/catch block, since int('3.2') (or any other float string) will raise an error. For example:

try: num = int(input('Enter a number...'))
except ValueError:
   print 'We only accept integers.'
   return
Will
  • 1,621
  • 15
  • 20
0

As many have suggested, you should use a try/except block. However if you want to accept user input like "6.12" and just calculate from the integer part, you should do:

user_in = "6.12" # or whatever the result from the input(...) call is
user_in = int(float(user_in)) # 6

int cannot operate on a string that's not integer-like, but it CAN operate on a floating point number. Calling float on the string will give you a floating point number, and calling int on that floating point number will return the integer part.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
-1
def main():
    # take input from the user
    num = float(input("Enter a number: "))
    if (num%1 != 0):
        print("Bad entry, only integers are accepted.")
        return 

    num = int(num)
    factorial = 1
    if num > 100:
        print("Bad entry. It should be an integer less than or equal to 100!")
        print("Please try again: ")
    elif num == 0:
        print("The factorial of 0 is 1")
    elif num < 0:
        print("Bad entry. It should be an integer superior than or equal to 0!")
        print("Please try again: ")  
    else:
        for i in range(1,num + 1):
            factorial = factorial*i
        print("The factorial of",num,"is",factorial)

main()
Dan Rasmuson
  • 5,705
  • 5
  • 29
  • 45
  • Can you add some explanation? Bare code blocks are bad. – Adam Smith Feb 06 '15 at 16:51
  • The module operator '%' will return the remainder of the operation. So, 9%1 is 0 while 9.1%1 is .1. Once we have determined if its float you can convert it to an int. – Dan Rasmuson Feb 06 '15 at 17:14
  • Seems unnecessary. You should just try to convert to `int` and catch the `ValueError` if it fails. The only case you're saving yourself here is if the user gives you a float it will trigger a DIFFERENT error message than the bare traceback if they give you a non-number. – Adam Smith Feb 06 '15 at 17:16
  • If you convert a float to an int it wont fail. It will truncate the number. – Dan Rasmuson Feb 06 '15 at 17:19
  • I suggest either A) convert to float then to int and use the resulting integer regardless of what the user entered, or B) do not convert to float, because `int("6.2")` DOES fail. – Adam Smith Feb 06 '15 at 17:21
  • How would you put this in a while loop? @DanielRasmuson – Miteux Feb 06 '15 at 18:58
  • 'while: main()' you could also have main return if it should continue. 'while main(): pass' – Dan Rasmuson Feb 06 '15 at 19:01