0

Here is my code, why is 0 outputting instead of entered value?

cube = 0
value = int(input("Enter a value to be cubed or 0 to quit: "))

while value != 0:
    cube = value **3
    value = int(input("Enter a value to be cubed or 0 to quit: "))

print (value, "cubed is", cube)
1
  • 1
    because you don't print until you exit the loop. Rearrange your statements to print in the loop after calculating. Commented Feb 13, 2022 at 3:22

1 Answer 1

1

First thing you want to do is indent that print, so that it refers to the values inside the loop:

cube = 0
value = int(input("Enter a value to be cubed or 0 to quit: "))

while value != 0:
    cube = value **3
    value = int(input("Enter a value to be cubed or 0 to quit: "))
    print (value, "cubed is", cube)

But now every time you print the result, cube refers to the previous round. So you want to change the order:

# no need to do cube = 0, it contributes nothing
value = int(input("Enter a value to be cubed or 0 to quit: "))

while value != 0:
    cube = value**3
    print (value, "cubed is", cube)
    value = int(input("Enter a value to be cubed or 0 to quit: "))
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh ok that makes sense, thank you for explaining where my mistake was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.