Stuck on loops question code

I’m stuck on this loops question. if anyone can help please to write the code.
Task:
Create a program using a while loop that keeps asking a user to enter a number and adds those numbers together to a total. When the total goes over 100, the program stops and outputs: “OVERLOAD! You have gone over 100!”

So far this is what i have written:

x = int(input("Enter a number"))
y = int(input("Enter another number"))
z = None
while True:
    z = x + y
    
    if z <= 150:
        print("your total is", z)
        print("Enter another number")
        w = int(input())
        print( z + w )
    else:
        print("your total is", z)
        print("OVERLORD")

thanks for all the help

Why does it ask for the first 2 numbers before the loop? Ask for all of them in the loop, one at a time.

Your code only ever adds those 2 initial numbers together, and it does so repeatedly. It doesn’t add any new number to the total. The total is always the same, namely, the result of x + y.

Your code should stop when the total exceeds 100, and you’re checking whether it exceeds 150, and even if it does exceed that value, you’re not breaking out of the loop.

And finally, the variable names aren’t meaningful. You really need only 2 of them; personally I’d call them number and total.

Thank you for your help, i will re write me code and hopefully it shall work

while True:
    Number1 = int(input("Enter a number"))
    Number2 = int(input("Enter another number"))
    answer = Number1 + Number2
    print(answer)
    
    if answer < 100:
        print("Enter number")
        Number3 = int(input())
        while answer < 100:
            answer += Number3
    else:
        print("overload")

why does this code not work. It does not continuosly add up the numbers. it will only add the first two numbers, then the next two numbers seperately. Any help?

It’s doing exactly what you’re telling it to do: ask for 2 numbers; ask for another number and keep adding it until the limit is reached; repeat all of that.

Try following it line by line, with a pencil and paper if necessary.

1 Like

Yes ok and what is that line of code that will allow it to keep adding by its self. in which part of my code is it wrong and stops the continous adding up. thanks

Basically, it should be like this:

total is 0
repeat
    ask for number
    add number to total
    print total
    if total too big
        print overload message
        break out of loop

but in Python.

thanks for that, ill edit my code and be back with you

hi there, your code you have sent me would work perfectly but you are implying that the numbers given by the user will be bigger than 100. what the numbers are smaller than 100, then the code should say ‘enter another number’ and will add that to the original number and continue to do so untill overload. that is where a while loop needs to be introduced and is also where i need help.
thanks

Hello Matthew, i have figured out the code and the problems. this is my final piece:

Number3 = None

Number1 = int(input("Enter a number"))
Number2 = int(input("Enter another number"))
answer = Number1 + Number2
print(answer)

while answer <= 100:
    Number3 = int(input("Enter another number"))
    answer = answer + Number3
    print(answer)


if answer >= 100:
    print("OVERLOAD")

it will work as suppose to. thank you for you help. I apprectiate your time and effort

A final couple of points:

  1. When it exits the loop, answer will be over 100, so the if condition at the end is guaranteed to be true, and, therefore, there’s no need to check whether it’s true.

  2. Your original post says that the final message should be “OVERLOAD! You have gone over 100!”, but your code prints only “OVERLOAD”.

Thank You :pray:

You could optimize your code and also do a little error checking, as error checking any user input is a fundamental requirement.

One way would be thus:

total = 0
while total <= 100:
    try:
        user_input = int(input("Integer> "))
        total += user_input
        print(f"Your total: {total}")
    except ValueError:
        print("Value Error")

print("OVERLOAD! You have gone over 100!")
1 Like

thanks Rob,
this is something i never thought of
:pray::pray:

You’re welcome; only to happy to help.

1 Like

Indeed. I’ve got some comments on the example though.

I’d be writing the try/except like this:

 try:
     user_input = int(input("Integer> "))
 except ValueError as e:
     print("Value Error:", e)
 else:
     total += user_input
     print(f"Your total: {total}")

There are 2 changes there:

  • second, the print recites the exception - you almost always want to
    report exactly what exeception occurred - it might not be what you
    expected
  • first, the try/except surrounds the smallest possible piece of code so
    that you have confidence that it came from the user_input = line

Narrow try/excepts are generally better, otherwise the exception might
have come from almost anything.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Thank you for your comments; very informative.

Some more ideas. I would prefer move user_input outside the try…except, this way more useful information can be provided to user. It also could be good idea to have only conversion to int under try. Something like:

user_input = input("Enter integer> ")

try:
    num = int(user_input)
except ValueError:
     print(f"Expected integer but {user_input!r} was entered")
else:
     # do something with num if ValueError is not raised