-1

Python beginner here. Practicing user input control. Trying to make user input loop to the beginning if anything but a whole number between 1 and 10 is used. Been trying for hours, tried using Try and Except commands but couldn't do it correctly. What am i doing wrong? Thank you.

Edit: Thank you very much for your help everyone, however the problem is still not solved (but very close!) I'm trying to figure out how to loop back to the beginning if anything BUT a whole number is typed. Agent Biscuit (above) gave a great answer for floating numbers, but any word or letter that is typed still produces an error. I´m trying to understand how to loop when anything random (except whole numbers between 1 and 10) is typed. None of the above examples produced corrcct results. Thank you for your help

while True:
    print("Enter a number between 1 and 10")
    number = int(input())
    if (number > 0) and (number < 10):
        print("Thank you, the end.")
        break
    else number != (> 0 and < 10):
        print("It has to be a whole number between 1 and 10.")
        print("Please try again:")
Peter
  • 11
  • 4

3 Answers3

0

I have identified some problems.

First, the input statement you are using would just raise an error if a float value is entered, because the int at the start requires all elements of the input to be a number, and . is not a number.

Second; your else statement. else is just left as else:, and takes no arguments or parameters afterwards.

Now, how to check if the number is not whole? Try this:

while True:
    print("Enter a number between 1 and 10")
    number = float(input())
    if (number > 0) and (number < 10) and (round(number)==number):
        print("Thank you, the end.")
        break
    else:
        print("It has to be a whole number between 1 and 10.")
        print("Please try again:")

This accepts a float value, but only accepts it if it is equal to a whole number, hence the (round(number)==number).

Hope that answers your question.

Agent Biscutt
  • 712
  • 4
  • 17
0

First of all, you can't use a condition in a else statement. Also, you need to use or operator instead of and if one of the conditions is acceptable.
So, your code needs to be like this

while True:
    print("Enter a number between 1 and 10")
    number = int(input())
    if (number > 0) and (number < 10):
        print("Thank you, the end.")
        break
    elif number < 0 or number >10:
        print("It has to be a whole number between 1 and 10.")
        print("Please try again:")
Python learner
  • 1,159
  • 1
  • 8
  • 20
0

Thanks to ack (above) for pointing me to a useful link. By studying another thread, I found the solution. It may not be perfect code, but it works 100%:

while True:
    try:
        print("Enter a number between 1 and 10")
        number = float(input())
        if (number > 0) and (number < 10) and (round(number)==number):
            print("Thank you, the end.")
            break
        else:
            print("\n")
            print("It has to be a whole number between 1 and 10.")
            print("Please try again:")
            print("\n")
            continue
    except ValueError:
        print("It has to be a whole number between 1 and 10.")
        print("Please try again:")
        print("\n")
Peter
  • 11
  • 4