-1

I am very new to programming and Python. To get started I'm working on a little game that will ask the user for some input and then do "something" with it. My problem is I've seem to account for if the user types in an int lower or high than my parameters BUT i can't seem to find a way to re-prompt the user if they type in anything but an int.

With my limited knowledge I thought that when using an if/elif/else statement if you didn't define what the if/elif is looking for than the else statement was there for everything else that you didn't account for?

Looking for some more insight on how to master this fundamental concept

Thank you in advance!

prompt = True
 while prompt == True:
 user_input = input("Please give me a number that is greater than 0 but less than 10 \n >")
if  user_input > 0 and user_input <= 10:
    print("Good job " + str(user_input)  +  " is a great number")
    break

elif (user_input  > 10):
    print("Hey dummy " + str(user_input) + " is greater than 10")

elif (user_input  <= 0):
    print("Hey dummy " + str(user_input) + " is less than 0")

else:
    print("I have no idea what you typed, try again!")
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Andy_L
  • 1
  • I tried your code and got `IndentationError: unexpected indent`. – melpomene Sep 13 '18 at 20:47
  • To handle input values properly, see this post https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers – user7504939 Sep 13 '18 at 20:48
  • Are you actually using python 3? `input()` always returns a `str`, so the way your code is written and your question make me wonder if you are accidentally running it with python 2.x – FatalError Sep 13 '18 at 20:49
  • I think your issue here is due to not converting the user's (string) input into an int before comparing. – Robin Zigmond Sep 13 '18 at 20:50
  • exhaustive answers to the same problem:[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) – Patrick Artner Sep 13 '18 at 20:50

1 Answers1

1

How about something like this?

a = -1 
while a < 0 or a > 10:
    try:
        a = int(input("Enter a number between 0 and 10: "))
    except ValueError:
        continue

This will only allow the user to enter an int from 0 to 10, this will also remove the need to print those messages if the number is outside of this range, if you would like to keep those messages I could make adjustments and show you how to handle that as well

vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20