Python 毕达哥拉斯

Python pythagoras

如果您不输入数字,我正在尝试获取 运行 的程序,但我不明白。 有人可以帮忙吗?

loop=True
print("Velcome to the pythagorean triples generator, if you want the machine to stop type stop")
print("Choose an uneven number over 1")
while loop:
    number1 = input("write here: ")
    
    try:
        number = int(number1)
    except:
        print("It has to be a whole number")
        
    if int(number)%2 == 0 or int(number)==1 
        print("the number has to be uneven and bigger than 1")
    
    else:
        calculation = int(number) ** 2
        calculation2 = int(calculation) - 1
        calculation3 = int(calculation2) / 2
        print ("Here is your first number,", int(calculation3))
        calculation4 = int(calculation3) + 1
        print ("Here is your second number,", int(calculation4))
    
    if str(tal) == "stop":
        break

编辑:翻译

loop = True
print("Velkommen til pythagoras tripler generatoren, hvis du vil stoppe maskinen skal du bare skrive stop")
print("Vælg et ulige tal over 1")
while loop:
    tal = input("Skiv her: ")

    try:
        number = int(tal)
    except Exception as e:
        print("Det skal være et tal")
        raise e // You should raise a Exception to let this program stops here.

    if int(number) % 2 == 0 or int(number) == 1: // You lack one ':'
        print("tallet skal være ulige og større end 1")

    else:
        udregning = int(number) ** 2
        udregning2 = int(udregning) - 1
        udregning3 = int(udregning2) / 2
        print("Her er dit ene tal,", int(udregning3))
        udregning4 = int(udregning3) + 1
        print("Her er dit andet tal,", int(udregning4))

    if str(tal) == "stop":
        break

我猜您希望程序在用户输入的不是数字时继续询问数字。

如果是这种情况,您应该澄清这一点,那么这样做就可以了:

except:
    print("It has to be a whole number")
    continue

continue关键字跳过当前迭代并继续下一次迭代。 The continue keyword in Python

如果不这样做,您的代码将在您的异常中打印“必须是数字”,但会在您尝试转换 number 的下一行继续执行,此时我们知道不能转换为 int,从而导致未处理的异常。这将按照我的建议使用 continue 关键字来解决。

但是,如果没有出现异常(即输入可以解释为 int),那么此时将 int(number) 说成 number 绝对没有意义点已经是 int!