【发布时间】:2014-06-17 20:53:45
【问题描述】:
我正在尝试根据书籍作业编写一个小程序,但我无法检测用户的输入是int/float(总数的增量)还是string(return error)。我尝试在 add_to_total 变量上使用 .isdigit(),但是当我输入浮点数时,它会直接跳到 else 代码块。我尝试在互联网上搜索,但找不到明确的答案。这是我的代码:
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
任何答案将不胜感激。
【问题讨论】:
标签: python variables methods integer int