The input takes numbers until a negative number is entered, every number before that is multiplied by 2 and the result is printed and formatted to the 2nd digit after the comma. I've tried the following, however the loop just continues endlessly giving me only 1 result, how can I make it stop and multiply with the next input instead?
x = float(input())
while x > 0:
result = x * 2
if x < 0:
print('Negative number!')
print(f'Result: {result:.2f}')
1 Answer 1
There are many issues with your code. Here's the fixed version:
while True:
x = float(input("Input: "))
if x < 0:
print('Negative number!')
break
else:
result = x * 2
print(f'Result: {result:.2f}')
"break" is needed to interrupt the while loop on the first bad result.
answered Nov 19, 2021 at 15:47
Some Guy
8191 gold badge10 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Vexarez
Thank you very much for the detailed answer, I will surely take another look at the loop and fix my mistakes. I was not entirely sure how the entire thing should look like however this has shined some light.
Explore related questions
See similar questions with these tags.
lang-py
if x < 0:will never be true, because you only go into the loop whenx > 0