Python毕达哥拉斯



我正试图得到一个程序运行,如果你不输入一个数字,但我没有得到它。有人能帮忙吗?

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

翻译编辑:

我猜你想让你的程序继续询问一个数字,当用户输入它不是一个数字。

如果是这种情况,你应该澄清,然后这样做:

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

continue关键字跳过当前迭代并继续进行下一次迭代。Python中的continue关键字

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

然而,如果没有引发异常(即输入可以解释为int),那么说int(number)number在此时已经是int是绝对没有意义的!

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

最新更新