我怎样才能 DRY 一个代码,它接受多个输入,对每个输入执行相同的功能,并一次给出所有输出?
How can I DRY a code which takes in several inputs, executes the same function for each input, and gives all for outputs at once?
我正在编写一个代码,可以同时计算和显示 4 条跑道的侧风分量 time.Between 输入(选定跑道)和输出(侧风分量),我有 4 个代码块对每条跑道使用相同的计算。为了简单起见,我写了一个replex:
first_runway = int(input("Enter the 1st runway"))
second_runway = int(input("Enter the 2nd runway"))
third_runway = int(input("Enter the 3rd runway"))
fourth_runway = int(input("Enter the 4th runway"))
crosswind1 = first_runway * 2
crosswind2 = second_runway * 2
crosswind3 = third_runway * 2
crosswind4 = fourth_runway * 2
print(crosswind1, crosswind2, crosswind3, crosswind4)
有没有办法只使用一次“*2”?
很简单Python。你可以这样做:
value = []
for i in range(1, 5):
runway = int(input("Enter the {}st runway".format(i)))
crosswind = runway * 2
value.append(crosswind)
print(value)
我正在编写一个代码,可以同时计算和显示 4 条跑道的侧风分量 time.Between 输入(选定跑道)和输出(侧风分量),我有 4 个代码块对每条跑道使用相同的计算。为了简单起见,我写了一个replex:
first_runway = int(input("Enter the 1st runway"))
second_runway = int(input("Enter the 2nd runway"))
third_runway = int(input("Enter the 3rd runway"))
fourth_runway = int(input("Enter the 4th runway"))
crosswind1 = first_runway * 2
crosswind2 = second_runway * 2
crosswind3 = third_runway * 2
crosswind4 = fourth_runway * 2
print(crosswind1, crosswind2, crosswind3, crosswind4)
有没有办法只使用一次“*2”?
很简单Python。你可以这样做:
value = []
for i in range(1, 5):
runway = int(input("Enter the {}st runway".format(i)))
crosswind = runway * 2
value.append(crosswind)
print(value)