我有点像Python n00b而且我无法结束我的python程序 - 它是毕达哥拉斯计算器。我不确定为什么,但是当我进入' n'时它似乎并不想结束。我的源代码如下:
from time import sleep
import math
while True:
print("Welcome to the pythagoras calculator!")
sleep(1)
print("This calculator will only calculate side lengths!")
sleep(1)
print("Is the side you want:\n 1: The hypotenuse \n Or \n 2: Not the hypotenuse \n Please enter your response")
sleep(0.5)
sideType = input()
try:
sideType = int(sideType)
pass
except:
print("Error - invalid input \n Please re-enter your data")
continue
sleep(0.5)
print("Thank you.")
if sideType == 1:
print("Please enter the length of one side")
sideOne = input("")
try:
sideOne = float(sideOne)
pass
except:
print("Error - invalid input \n Please re-enter your data")
continue
sleep(0.5)
print("Please enter the length of the other side")
sideTwo = input()
try:
sideTwo = float(sideTwo)
pass
except:
print("Error - invalid input \n Please re-enter your data")
continue
sleep(0.5)
print("Thank you.")
sleep(0.5)
answerForHypLen = math.sqrt(math.pow(sideOne,2)+math.pow(sideTwo,2))
print("Calculations complete: \n Your answer is: " + str(answerForHypLen))
elif sideType == 2:
print("Please enter the length of the hypotenuse")
sideHyp = input("")
try:
sideHyp = float(sideHyp)
pass
except:
print("Error - invalid input \n Please re-enter your data")
continue
sleep(0.5)
print("Please enter the length of the other side")
sideNotHyp = input()
try:
sideNotHyp = float(sideNotHyp)
pass
except:
print("Error - invalid input \n Please re-enter your data")
continue
sleep(0.5)
print("Thank you.")
sleep(0.5)
answerForNotHypLen = math.sqrt(math.pow(sideHyp,2)-math.pow(sideNotHyp,2))
print("Calculations complete: \n Your answer is: " + str(answerForNotHypLen))
sleep(0.5)
print("Would you like to continue?")
contYN = input()
if contYN == 'Y'or 'y':
continue
elif contYN != 'Y' or 'y':
False
答案 0 :(得分:3)
您需要更改这些行:
if contYN == 'Y'or 'y':
continue
elif contYN != 'Y' or 'y':
False
要:
if contYN.upper() == 'Y':
continue
else:
break