On 29/03/13 11:33, Ghadir Ghasemi wrote:
Hi guys I am trying to create part of a vending machine.
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,
Yes you can use try/except to catch the failure.
if it isn't the program just keeps asking for an input?
and a while loop will keep you circulating.
You may not have come across these in your tutorial/course yet
fiftypencecoins = int(input("how many 50p coins do you want to insert or press 'e'
to exit : "))
if fiftypencecoins == 'e':
break
But this check needs to be before you convert to int() otherwise the 'e'
will always raise an error when you try to convert it!
else:
currentmoney += fiftypencecoins * 5/10
print("your newly updated credit is £" + str(currentmoney) + "0")
So in pseudo code you should have
while True:
read input
if exit condition break
try: convert to int
except ValueError: continue
process the value
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor