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")
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.
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.
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
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
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.
Your original post says that the final message should be “OVERLOAD! You have gone over 100!”, but your code prints only “OVERLOAD”.
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!")
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