今天早些时候刚开始学习python,我不明白如果输入"N“,为什么这段代码不会结束程序。另外,如果我将最后一条if语句改为!= "Y“,那么即使我键入Y,它也会关闭,这让我非常困惑。我只是想做一个简单的程序来转换华氏温度到摄氏度,如果系统提示的话,不需要查看任何代码,所以如果它写得很糟糕,这就是为什么。
while True:
def temp_func():
print("Is the temperature you'd like to convert in Fahrenheit or Celcius?")
temp = input()
if temp == "Fahrenheit":
F_temp = int(input("Please write the temperature."))
converted_temp_F = ((F_temp - 32) * 5 / 9)
print(str(converted_temp_F))
elif temp == "Celcius":
C_temp = int(input("Please write the temperature."))
converted_temp_C = ((C_temp * (9 / 5)) + 32)
print(str(converted_temp_C))
temp_func()
print(input("Would you like to convert another temperature? (Y/N) "))
if input == 'Y':
True
if input == 'N':
break
print ("Goodbye")发布于 2020-07-26 16:14:06
更换您的打印(输入...和条件:
response = input("Would you like to convert another temperature? (Y/N) ").upper()
if response == 'N':
break发布于 2020-07-26 16:11:36
您需要将用户给定的输入值存储在Y/N的变量中。您应该将该函数移出while循环
def temp_func():
print("Is the temperature you'd like to convert in Fahrenheit or Celcius?")
temp = input()
if temp == "Fahrenheit":
F_temp = int(input("Please write the temperature."))
converted_temp_F = ((F_temp - 32) * 5 / 9)
print(str(converted_temp_F))
elif temp == "Celcius":
C_temp = int(input("Please write the temperature."))
converted_temp_C = ((C_temp * (9 / 5)) + 32)
print(str(converted_temp_C))
while True:
temp_func()
Y_N = input("Would you like to convert another temperature? (Y/N) ")
if Y_N == 'N':
break
print ("Goodbye")发布于 2020-07-26 16:13:58
试着这样做
def temp_func():
print("Is the temperature you'd like to convert in Fahrenheit or Celcius?")
temp = input()
if temp == "Fahrenheit":
F_temp = int(input("Please write the temperature."))
converted_temp_F = ((F_temp - 32) * 5 / 9)
print(str(converted_temp_F))
elif temp == "Celcius":
C_temp = int(input("Please write the temperature."))
converted_temp_C = ((C_temp * (9 / 5)) + 32)
print(str(converted_temp_C))
while True:
temp_func()
x = input("Would you like to convert another temperature? (Y/N) ")
if x.lower() == "n":
break
print ("Goodbye")https://stackoverflow.com/questions/63097644
复制相似问题