loop a function until it reaches a threshold then finishes the script

Viewed 191

I've got the task of looping a function that allows for an imaginary person to spend up to £100 or whatever currency until the 100 runs out in which case the script just ends. While the script is running it adds up each of the values and keeps track until the threshold is met!

#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 Answers
while True:
    do_stuff()
    if you_want_to_stop:
        break
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
Related