0

My goal is creating a factorial program in python that asks infinite user input to find factorial of the number user type, until I would like to quit the program. But there is probably discrepancy between the lines of the code to works for exit the program and integer numbers below it.

1) I tried to solve this to not write int(input) I wrote just

input('Enter a number. Type exit to stop:> ')

both above or below the while True statement but it didn't work.

2) I also want to use lower() function to quit the program but when I use it, the discrepancy happens again because I ask user input for an integer but when I turn it to a normal input and type it the above while True statement, problem occurs.

3) Also I want to user input as a number with using that isdigit() function tried to use like this but it didn't work well:

factorial = 1
user_input = input('Enter a number: ')
while user_input.lower() != 'exit':

    while not user_input.isdigit():
        print('This is not a number. Please try again:> ')
        user_input = int(input('Try again:> '))
    user_input = int(input('Enter a new number: '))
   .
   .
   .

and this, too didn't work

My actual code is this:

while True:

factorial = 1
user_input = int(input('Enter a number. Type exit to stop:> '))

if user_input == 'exit':
    print('You are quitting the program')
    break

elif user_input < 0:
    print("Sorry, factorial does not exist for negative numbers")

elif user_input == 0:
    print("The factorial of 0 is 1")

else:
    for i in range(1, user_input + 1):
        factorial = factorial * i
    print("The factorial of", user_input, "is", factorial)

The program works like this:

Enter a number. Type exit to stop:> 4
The factorial of 4 is 24

Enter a number. Type exit to stop:> 5
The factorial of 5 is 120

Enter a number. Type exit to stop:> 6
The factorial of 6 is 720

and when I type 'exit' to quit from program I am receiving this kind of error:

Traceback (most recent call last):
  File "E:/Kodlar/Python/Taslak projeler/taslak177.py", line 5, in <module>
    user_input = int(input('Enter a number. Type exit to stop:> '))
ValueError: invalid literal for int() with base 10: 'exit'

As you can see, code blocks work instead of quitting the program with user input. How can I fix this?

Can anyone help? Thanks already!

Edit: I reorganized the code and it works perfectly fine. Thanks for your all responses!

while True:
    user_input = input("Enter a number:> ")
    if user_input == "exit":
        print('You are quitting the program...')
        break
    else:
        try:
            factorial = 1
            user_input = int(user_input)
            if user_input < 0:
                print("Sorry, factorial does not exist for negative numbers")
            elif user_input == 0:
                print("The factorial of 0 is 1")
            else:
                for i in range(1, user_input + 1):
                    factorial = factorial * i
                print(f'The factorial of {user_input} is {factorial}')
        except ValueError:
            print("Please provide a valid number")
Enis
  • 3
  • 4
  • `int('exit')` is not possible. – Austin Apr 30 '20 at 12:57
  • 2
    You need to *first* check if the input is `'exit'` and *then* (if it isn't) try to convert it to an integer. – mkrieger1 Apr 30 '20 at 12:58
  • you convert the user input using `int()` before you check whether it is `exit`, which is a string. First check whether it is `exit`. – amdex Apr 30 '20 at 12:58
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – mkrieger1 Apr 30 '20 at 12:58

3 Answers3

0

Your program get int inputs,

user_input = int(input('Enter a new number: '))

try this instead, get string input

user_input = input('Enter a new number: ')

and convert it into int later

user_input = int(user_input)
Avishka Dambawinna
  • 1,180
  • 1
  • 13
  • 29
0

You should check if the input is exit before converting it to int and if so, break the loop.

Try this instead:

while True:
    user_input = input("Enter a number:")
    if user_input == "exit":
        print('You are quitting the program')
        break
    else:
        try:
            user_number = int(user_input)
            if user_number < 0:
                print("Sorry, factorial does not exist for negative numbers")
            elif user_number == 0:
                print("The factorial of 0 is 1")
            else:
                # calc factorial here
        except ValueError:
            print("Please provide a valid number")

Gabio
  • 9,126
  • 3
  • 12
  • 32
  • It helped a lot, thanks! Instead of user_number I tried to type user_input. Then I described user_input as an integer value below the try section and problem solved! – Enis Apr 30 '20 at 15:26
0

Because you are casting the user's response (a string) into an int in both cases.

    user_input = int(input('Enter a new number: '))

and later

user_input = int(input('Enter a number. Type exit to stop:> '))

Perhaps try a little tweak:

while True:

    factorial = 1
    user_input = input('Enter a number. Type exit to stop:> ')

    if user_input.lower().strip() == 'exit':
        print('You are quitting the program')
        break

    elif user_input.isnumeric() and user_input < 0:
        print("Sorry, factorial does not exist for negative numbers")

    elif user_input.isnumeric() and user_input == 0:
        print("The factorial of 0 is 1")

    elif user_input.isnumeric():
        for i in range(1, user_input + 1):
            factorial = factorial * i
        print("The factorial of", user_input, "is", factorial)
    else:
        print("Please enter an integer or 'exit'")

You could also wrap another if so you don't duplicate the isnumeric() tests

Mr Felix U
  • 295
  • 1
  • 11