1

I'm quite new to programming. I was trying to figure out how to check if a user input (which get stores as a string) is an integer and does not contain letters. I checked some stuff on the forum and in the end came up with this:

while 1:

#Set variables
    age=input("Enter age: ")
    correctvalue=1

#Check if user input COULD be changed to an integer. If not, changed variable to 0
    try:
        variable = int(age)
    except ValueError:
        correctvalue=0
        print("thats no age!")

#If value left at 1, prints age and breaks out of loop.
#If changed, gives instructions to user and repeats loop.
    if correctvalue == 1:
        age=int(age)
        print ("Your age is: " + str(age))
        break
    else:
        print ("Please enter only numbers and without decimal point")

Now, this works as shown and does what I want (ask the persons age untill they enter an integer), however is rather long for such a simple thing. I've tried to find one but I get too much data that I don't understand yet.

Is there a simple shorter way or even a simple function for this to be done?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
LuukV
  • 203
  • 1
  • 11

4 Answers4

2

You can make this a little shorter by removing the unnecessary correctvalue variable and breaking or continue-ing as necessary.

while True:
    age=input("Enter age: ")    
    try:
        age = int(age)
    except ValueError:
        print("thats no age!")
        print ("Please enter only numbers and without decimal point")
    else:
        break

print ("Your age is: " + str(age))
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I guess yours _is_ shorter than mine by one line :) If the `except` contains a `continue`, why use `else`? – Tom Fenech May 02 '14 at 13:54
  • Good question :) I think at one point I still had some code following the try statement, but forgot to remove the `continue` when I removed it. – chepner May 02 '14 at 14:23
1

Use isdigit()

"34".isdigit()
>>> "34".isdigit()
True
>>> "3.4".isdigit()
False
>>> 

So something like this:

while True:

    #Set variables
    age=input("Enter age: ")

    #Check 
    if not age.isdigit():
        print("thats no age!")
        continue

    print("Your age is: %s" % age)
    age = int(age)  
    break
Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41
  • Although it would do the trick for this specific age scenario, it doesn't handle negative numbers. Currently the question title ask for an integer check. – Xyand May 02 '14 at 13:36
  • And this is not better according to most philosophies/conventions. – Oleh Prypin May 02 '14 at 13:38
0

Your code can be made a bit shorter like this. I was going to suggest changing your correctvalue variable from an integer 1 or 0 to a boolean True or False but it is redundant anyway. continue can be used to repeat the loop as necessary.

while True:

    age = input("Enter age: ")

    try:
        age = int(age)
    except ValueError:
        print("That's no age!")
        print("Please enter only numbers and without decimal point")
        continue

    print ("Your age is: " + str(age))
    break
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
0

This works for nonnegative integers (i.e., without a sign marker):

variable = ''
while True:
    variable = input("Age: ")
    if variable.isdigit():
        break
    else:
        print("That's not an age!")
variable = int(variable)

The idea is that you loop continuously until the user inputs a string that contains only digits (that's what isdigit does).

lmjohns3
  • 7,422
  • 5
  • 36
  • 56
  • You do not need `variable = ''` at the top of the while loop. I also think `try: int(x) except ValueError:` form is better in this case – dawg May 19 '14 at 17:31