Python -1在多维数据集根计算器中返回错误

Python -1在多维数据集根计算器中返回错误,python,python-3.x,Python,Python 3.x,这是我的立方根计算器: cube = int(input('Input an integer: ')) for guess in range(abs(cube + 1)): if guess ** 3 >= abs(cube): break if guess ** 3 != abs(cube): print(str(cube) + ' is not a perfect cube.') else: if cube < 0: gue

这是我的立方根计算器:

cube = int(input('Input an integer: '))
for guess in range(abs(cube + 1)):
    if guess ** 3 >= abs(cube):
        break
if guess ** 3 != abs(cube):
    print(str(cube) + ' is not a perfect cube.')
else:
    if cube < 0:
        guess = - guess
    print('Cube root of ' + str(cube) + ' is ' + str(guess))
它打印所有负整数的预期答案,除了
-1
,我无法找到这种行为的原因。它应该将
-1
打印为输出,当
range()
函数“定义”它时,我不认为有任何理由定义
guess


你看到我缺少什么了吗?

当你在函数中输入-1时,for循环永远不会运行
abs(-1+1)
为0,因此它从不运行,因此guess从不初始化。如果
cube==-1
,则可能需要执行
abs(-1)+1

,然后
abs(cube+1)==0
,并且
范围(abs(cube+1))
为空。因此,没有迭代发生(因为没有迭代),并且永远不会创建名称
guess


而且,
范围
不是一个函数,它是一个函数。

您必须正确缩进
guess
仅在for循环的范围内定义,它是for循环的索引

cube = int(input('Input an integer: '))
for guess in range(abs(cube + 1)):
    if guess ** 3 >= abs(cube):
        break
if guess ** 3 != abs(cube): # this if is outside the for loop so guess is not defined
    print(str(cube) + ' is not a perfect cube.')
else: # this else-if is outside the foor loop so guess is not defined
    if cube < 0:
        guess = - guess
    print('Cube root of ' + str(cube) + ' is ' + str(guess))
关于你想做什么,我最好的猜测如下。可能有更优雅的方法来编写程序,我知道
break
语句会使逻辑难以遵循,因此不受欢迎

cube = int(input('Input an integer: '))
for guess in range(abs(cube) + 1):
    # if a perfect cube is found we enter this if statement
    if guess ** 3 == abs(cube):
        # these if and else statements print the appropriate cube depending on if the input is negative or positive
        if cube < 0:
            print('Cube root of ' + str(cube) + ' is ' + str(guess * -1))
            break
        else:
            print('Cube root of ' + str(cube) + ' is ' + str(guess))
            break
    # else if the index cubed is greater than our input, we know it can't be a perfect cube and we should exit the loop.
    elif guess ** 3 > abs(cube):
        # tell the user the input is not a perfect cube
        print(str(cube) + " is not a perfect cube.")
        break
cube=int(输入('input a integer:'))
对于范围内的猜测(绝对值(立方体)+1):
#如果找到一个完美的立方体,我们就输入这个if语句
如果猜测**3==abs(立方体):
#这些if和else语句根据输入是负数还是正数打印相应的多维数据集
如果立方体<0:
print(“+str(Cube)+”的立方根是“+str(guess*-1))
打破
其他:
print(“+str(Cube)+”的立方根是“+str(guess))
打破
#否则,如果索引立方大于我们的输入,我们知道它不可能是一个完美的立方体,我们应该退出循环。
elif guess**3>abs(立方体):
#告诉用户输入不是一个完美的立方体
打印(str(cube)+“不是一个完美的立方体。”)
打破

输入值为-1时,您调用的是
范围(0)
,这将生成一个空序列-
循环的
主体从不执行,因此
guess
从不被赋值。我想你想要的是
abs(立方体)+1
cube = int(input('Input an integer: '))
for guess in range(abs(cube + 1)):
    if guess ** 3 >= abs(cube):
        break
    if guess ** 3 != abs(cube): # now it is inside the for loop
        print(str(cube) + ' is not a perfect cube.')
    else: # this is also now inside the for loop
        if cube < 0:
            guess = - guess
        print('Cube root of ' + str(cube) + ' is ' + str(guess))
cube = int(input('Input an integer: '))
for guess in range(abs(cube) + 1):
    # if a perfect cube is found we enter this if statement
    if guess ** 3 == abs(cube):
        # these if and else statements print the appropriate cube depending on if the input is negative or positive
        if cube < 0:
            print('Cube root of ' + str(cube) + ' is ' + str(guess * -1))
            break
        else:
            print('Cube root of ' + str(cube) + ' is ' + str(guess))
            break
    # else if the index cubed is greater than our input, we know it can't be a perfect cube and we should exit the loop.
    elif guess ** 3 > abs(cube):
        # tell the user the input is not a perfect cube
        print(str(cube) + " is not a perfect cube.")
        break