循环执行功能,直到达到阈值,然后完成脚本

时间:2018-11-08 12:42:55

标签: python loops

我的任务是循环一个函数,该函数允许虚构的人花费高达100英镑或任何货币,直到100用完为止,在这种情况下脚本才结束。在脚本运行时,它会累加每个值并一直跟踪直到达到阈值!

#This line should initialise a variable
while #I need Finish this line with a loop condition.
  x = int( input("How much is the next item? ") )
  tot = tot+x
print("You cannot afford that!  You only have £" + str(100-(tot-x)) + "

2 个答案:

答案 0 :(得分:2)

while True:
    do_stuff()
    if you_want_to_stop:
        break

答案 1 :(得分:1)

tot = 0
while True:
    x = int( input("How much is the next item? ") )
    if (tot+x)>100:
        print("You cannot afford that!  You only have £ {}".format(str(100-(tot))))
        continue #you want to skip this iteration and repeat
    if tot==100:
        print("You cannot afford more!")
        break #you want to stop
    tot += x