所以我只是在python3上写了一个基本的英里到公里转换器。它会询问您是否要在键入nautical时将海里或陆地英里转换为km,然后转到该循环并将海里转换为km。土地也是如此,如果您输入土地,它将土地英里转换为公里。但是问题是当我键入其他内容而不是航海或着陆时,程序崩溃了。我该如何写写“请仅在此输入中写上航海或陆地”的代码。当我在“要转换为公里数的英里数”中键入一个非数字的输入时,会发生相同的情况它崩溃了...
print("Hello! This is a mile to km converter")
print("Do you want to convert nauitcal miles or land miles?")
question_convert = input("Please type nautical or land \n")
if question_convert == "nautical":
NAUTICAL = True
if question_convert == "land":
NAUTICAL = False
while True:
while NAUTICAL == True:
nautical_mile = 1.852
print("How many nauitical miles do you want to convert in km?")
convert_nauitcal = float(input())
converted_nautical = float(convert_nauitcal * nautical_mile)
print("Here's your converted nauitical miles: ")
print(converted_nautical)
quit()
while NAUTICAL == False:
land_mile = 1.609344
print("How many land miles do you want to convert in km?")
convert_land = float(input())
converted_land = float(convert_land * land_mile)
print("Here's your converted land miles: ")
print(converted_land)
quit()
#land_mile = 1.609344
#nautical_mile = 1.852
答案 0 :(得分:1)
我将对其进行更改,并将问题部分也放入while语句内,以便您的用户只有输入有效的响应后才能继续操作。因此,调整后的代码如下:
print("Hello! This is a mile to km converter")
print("Do you want to convert nauitcal miles or land miles?")
valid_input=False
while not valid_input:
question_convert = input("Please type nautical or land \n")
if question_convert == "nautical":
NAUTICAL = True
valid_input=True
if question_convert == "land":
NAUTICAL = False
valid_input=True
while True:
while NAUTICAL == True:
nautical_mile = 1.852
print("How many nauitical miles do you want to convert in km?")
convert_nauitcal = float(input())
converted_nautical = float(convert_nauitcal * nautical_mile)
print("Here's your converted nauitical miles: ")
print(converted_nautical)
quit()
while NAUTICAL == False:
land_mile = 1.609344
print("How many land miles do you want to convert in km?")
convert_land = float(input())
converted_land = float(convert_land * land_mile)
print("Here's your converted land miles: ")
print(converted_land)
quit()
答案 1 :(得分:0)
这是因为您将其放入无限的while循环中。您可以通过修改if
既不是NAUTICAL
也不是True
的初始False
条件来解决此问题:
print("Hello! This is a mile to km converter")
print("Do you want to convert nauitcal miles or land miles?")
question_convert = input("Please type nautical or land \n")
NAUTICAL = None
if question_convert == "nautical":
NAUTICAL = True
else if question_convert == "land":
NAUTICAL = False
else:
print("I don't understand that...")
# maybe throw an exception here if you want, instead of exit()-ing
exit()
while NAUTICAL is not None:
while NAUTICAL:
nautical_mile = 1.852
print("How many nauitical miles do you want to convert in km?")
convert_nauitcal = float(input())
converted_nautical = float(convert_nauitcal * nautical_mile)
print("Here's your converted nauitical miles: ")
print(converted_nautical)
quit()
while not NAUTICAL:
land_mile = 1.609344
print("How many land miles do you want to convert in km?")
convert_land = float(input())
converted_land = float(convert_land * land_mile)
print("Here's your converted land miles: ")
print(converted_land)
quit()
#land_mile = 1.609344
#nautical_mile = 1.852
答案 2 :(得分:0)
对于您的第一个输入问题,可能会崩溃,因为除非输入是“航海”或“陆地”,否则未定义变量NAUTICAL。
一个解决这个问题的方法是
if question_convert=='nautical':
NAUTICAL = True
elif question_convert=='land':
NAUTICAL = False
else:
print('Please type land or nautical!!')
sys.exit(1)
对于其他输入验证问题,在这种情况下检查数字输入的最简单方法是在float转换周围使用try / except子句
...
in_val = input()
try:
converted_land = float(in_val)
except ValueError:
print("{} doesn't look like a number!!".format(inv_val))
# might want to exit at this point or use a default value for converted_land
...
答案 3 :(得分:0)
ALLOWED_TYPES = {'nautical', 'land'}
question_convert = ''
while question_convert not in ALLOWED_TYPES:
question_convert = input("Please type nautical or land \n")
if question_convert not in ALLOWED_TYPES:
print('"Please only write nautical or land in this input')
continue
if question_convert == "nautical":
NAUTICAL = True
if question_convert == "land":
NAUTICAL = False