这是我从事过的一本书中的一些代码,因为我是Python的新手....这部分是应该的
totalCost = 0
print 'Welcome to the receipt program!'
while True:
cost = raw_input("Enter the value for the seat ['q' to quit]: ")
if cost == 'q':
break
else:
totalCost += int(cost)
print '*****'
print 'Total: $', totalCost
share|edit
answered 8 hours ago
我的困境是....我需要验证输入是什么...所以如果用户输入一个字符串(比如单词'five'而不是数字)而不是q或数字它告诉他们“我是对不起,但'五'无效 . 请再试一次.....然后它会再次提示用户输入 . 我是Python的新手并且在这个简单的问题上绞尽脑汁
UPDATE** 由于我没有足够的学分来添加我自己的问题的答案所以日志我在这里发布....
Thank you everyone for your help. I got it to work!!! I guess it's going to take me a little time to get use to loops using If/elif/else and while loops.
This is what I finally did
total = 0.0
print 'Welcome to the receipt program!'
while True:
cost = raw_input("Enter the value for the seat ['q' to quit]: ")
if cost == 'q':
break
elif not cost.isdigit():
print "I'm sorry, but {} isn't valid.".format(cost)
else:
total += float(cost)
print '*****'
print 'Total: $', total