我需要优化我的代码,但我不知道该怎么做。我可以把fahr_inputs
都变成一个函数,但我不能让它工作
# fahr to cel conversion
def toCelsius (fahr):
cel = (fahr - 32) * 5/9
return float(cel)
#dispaly the input and average of fahrenheit and celsius
def displayFahr():
sum = fahr_input1 + fahr_input2 + fahr_input3 + fahr_input4 + fahr_input5
average = sum / 5
print ("Your fahrenheit numbers were: ",fahr_input1, fahr_input2, fahr_input3, fahr_input4, fahr_input5)
print ("The sum of fahrinheit is : ", sum)
print ("the average is: ", average)
fahr_input1 = int(input("Please enter a Fahrenheit temperature here: "))
fahr_input2 = int(input("Please enter a Fahrenheit temperature here: "))
fahr_input3 = int(input("Please enter a Fahrenheit temperature here: "))
fahr_input4 = int(input("Please enter a Fahrenheit temperature here: "))
fahr_input5 = int(input("Please enter a Fahrenheit temperature here: "))
displayFahr()
我试过了,但没用。你知道吗
def fahr_input ():
i = 0
while i < 5:
input1 = int(input("Please enter a Fahrenheit temperature here: "))
i + 1
return input1
将输入放入循环中:
while-loop
),但是如果你想要5个温度,一个for-loop
就足够了。你知道吗list comprehension
内收集temps
代码:
disply_fahr()
的输出:试试这个
相关问题 更多 >
编程相关推荐