while 循环输出的值不正确
incorrect value outputting from while loop
这是我的代码,为什么输出的是0而不是输入的值?
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)
您要做的第一件事是缩进 print
,以便它引用循环内的值:
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)
但是现在每次打印结果时,cube
指的是上一轮。所以你想改变顺序:
# 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: "))
这是我的代码,为什么输出的是0而不是输入的值?
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)
您要做的第一件事是缩进 print
,以便它引用循环内的值:
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)
但是现在每次打印结果时,cube
指的是上一轮。所以你想改变顺序:
# 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: "))