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

Amit Saha amitsaha.in at gmail.com
Fri Mar 29 12:35:27 CET 2013


On Fri, Mar 29, 2013 at 9:33 PM, Ghadir Ghasemi
<ghasemmg01 at leedslearning.net> 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")

Is this Python 3 or Python 2 ?

-Amit.

-- 
http://amitsaha.github.com/


More information about the Tutor mailing list