循环一个函数直到它达到阈值然后完成脚本

我的任务是循环一个函数,该函数允许想象中的人花费最多 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)) + "


MMTTMM
浏览 133回答 2
2回答

芜湖不芜

tot = 0while 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

慕田峪9158850

while True:     do_stuff()     if you_want_to_stop:             break
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python