按Enter键后如何删除空白输入行。
temp = input('Enter temperature: ')
if temp == '':
break
temp = int(temp)
temp_list.append(temp)
如果temp ==``:按Enter时将产生“ Enter temperature:”,没有任何输入(显然)。我想算出,当按Enter时,输入提示将被删除,并且不会显示任何内容,并且程序进入下一个循环。
如果我没有输入任何内容,只需按Enter键即可显示代码:
Enter temperature:
Enter windspeed:
Enter current humidity(0-100):
Average temperature: No info on temperature
Maximum wind speed: No info on wind speed
Current humidity: No info on humidity
我希望它在我什么也不做时显示此内容,只需按Enter键即可:
Average temperature: No info on temperature
Maximum wind speed: No info on wind speed
Current humidity: No info on humidity
完整代码:
temp_list = []
wind_list = []
humid_list = []
while True:
temp = input('Enter temperature: ')
if temp == '':
break
temp = int(temp)
temp_list.append(temp)
while True:
wind_speed = input('Enter windspeed: ')
if wind_speed == '':
break
wind_speed = int(wind_speed)
wind_list.append(wind_speed)
if wind_speed <= -1:
break
while True:
humid = input('Enter current humidity(0-100): ')
if humid == '':
humid_list.append(humid)
break
humid = int(humid)
humid_list.append(humid)
if (humid <= -1 or humid >= 101):
break
if len(temp_list) == 0:
print('Average temperature: No info on temperature')
else:
avg_temp = sum(temp_list) / len(temp_list)
print('Average temperature:', int(avg_temp))
if len(wind_list) == 0:
print('Maximum wind speed: No info on wind speed')
else:
print('Maximum wind speed:', max(wind_list))
if humid_list[0] == '' or humid_list[0] <= -1 or humid_list[0] >= 101:
print('Current humidity: No info on humidity')
elif humid_list[-1] <= -1 or humid_list[-1] >= 101:
print('Current humidity: ', humid_list[-2])
else:
print('Current humidity:', humid_list[-1])
这是代码运行了几次,并且可以正常工作。如果未输入任何内容,则尝试删除该条目。 Picture of IDLE showing no issues
答案 0 :(得分:0)
您应该使用比较运算符==而不是赋值运算符=