[Tutor] How to check if user input is an integer

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Mar 29 14:15:28 CET 2013


On 29/03/2013 11:33, Ghadir Ghasemi wrote:
> Hi guys I am trying to create part of a vending machine. The section below is if the user wants to insert 50p coins. It works but when I entered a non integer like 'dfsdf', the program just broke. Is there a way that the program can check if the input is an integer, and if it is, then the program does all the calculations, but if it isn't the program just keeps asking for an input? Thanks. Here is the section.
>
> fiftypencecoins = int(input("how many 50p coins do you want to insert or press 'e' to exit : "))
> if fiftypencecoins == 'e':
>          break
> else:
>       currentmoney += fiftypencecoins * 5/10
>       print("your newly updated credit is £" + str(currentmoney) + "0")
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Alan Gauld has already pointed you in the right direction.  But in 
future can you please give us the entire traceback or state exactly what 
the problem is.  "the program just broke" usually doesn't tell us much, 
although in this case it's easy enough to work out.  Also state your OS 
and Python version.

TIA.

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence



More information about the Tutor mailing list