jeudi 1 mai 2014

Python: True if variable is integer or float


Vote count:

0




I am trying to write a small program from a book assignment, but I am having trouble with detecting if the user's input is a int/float (increment to the total) or string (return error). I tried using .isdigit() on the add_to_total variable but when I type in a float, it skips straight to the else code block. I have tried searching on the internet but can't find a clear answer. Here is my code: total = 0



print("Welcome to the receipt program!")

while True:
add_to_total = raw_input("Enter the value for the seat ['q' to quit]: ")
if add_to_total == 'q':
print("*****")
print "Total: $%s" % total
break
if add_to_total.isdigit(): #Don't know how to detect if variable is int or float at the same time.
add_to_total = float(add_to_total)
total += add_to_total
else:
print "I'm sorry, but '%s' isn't valid. Please try again." % add_to_total


Any answer would be greatly appreciated.



asked 32 secs ago






Aucun commentaire:

Enregistrer un commentaire