gpt4 book ai didi

python - 如何使 IF 语句更有效?

转载 作者:行者123 更新时间:2023-12-04 08:42:22 25 4
gpt4 key购买 nike

我创建了一个小程序,它接受用户输入的“飞行规范”,并根据输入输出该飞行的详细信息。然而,它只是一堆if语句,我想知道是否有任何有用的技术来减少if语句的数量并使程序更高效。
代码:

import time

def main():

AMS_DESTINATION = "Schiphol, Amsterdam"
GLA_DESTINATION = "Glasgow, Scotland"
AMS_PRICE = 150.00
GLA_PRICE = 80.00

# User input [LLL 0 00 L L]
flightSpecification = str(input("Enter Flight Specification: "))
flightDestination = flightSpecification[0:3]

bagCount = int(flightSpecification[4])
baggageCost = float((bagCount - 1) * 20)

passengerAge = int(flightSpecification[6:8])

standardMeal = 10.00
vegetarianMeal = 12.00
mealType = str(flightSpecification[9])

seatClass = str(flightSpecification[11])

totalFlightCost = 0

if flightDestination.lower() != 'ams' and flightDestination.lower() != 'gla':
print("Please enter a valid flight specification! [LLL 0 00 L L]")
time.sleep(2)
main()

if flightDestination.lower() == 'ams':
print(f"Destination: {AMS_DESTINATION}")
print(f"Flight cost: £{AMS_PRICE}")
totalFlightCost = 150

elif flightDestination.lower() == 'gla':
print(f"Destination: {GLA_DESTINATION}")
print(f"Flight cost: £{GLA_PRICE}")
totalFlightCost = 80

print(f"Number of bags: {bagCount}")
print(f"Baggage Cost: £{baggageCost}")

if passengerAge > 15:
print("Child: False")

elif passengerAge <= 15:
print("Child: True")
totalFlightCost = totalFlightCost / 2
standardMeal = standardMeal - 2.50
vegetarianMeal = vegetarianMeal - 2.50

elif passengerAge < 0:
print("Age cannot be negative")
main()

if mealType == 'S' and seatClass != 'F':
totalFlightCost = totalFlightCost + standardMeal
print("Meal: Standard")
print(f"Meal Cost: £{standardMeal}")

elif mealType == 'V' and seatClass != 'F':
totalFlightCost = totalFlightCost + vegetarianMeal
print("Meal: Vegetarian")
print(f"Meal Cost: £{vegetarianMeal}")

elif mealType == 'N':
print("Meal: None")
print("Meal Cost: £0")

# THIS COULD DEFINITELY BE DONE MORE EFFICIENTLY
if seatClass == 'F':
if mealType == 'S':
totalFlightCost = totalFlightCost + standardMeal
print("Meal: Standard")
print(f"Meal Cost: FREE")

elif mealType == 'V':
totalFlightCost = totalFlightCost + vegetarianMeal
print("Meal: Vegetarian")
print(f"Meal Cost: £{vegetarianMeal}")

print("Seating Class: First")
totalFlightCost = totalFlightCost * 6

elif seatClass == 'E':
print("Seating Class: Economy")

print(totalFlightCost)



main()
谢谢

最佳答案

代码
除了您问题下方的评论外,我在您提到的可以优化的部分更改了一个小东西(我还删除了用户输入,因为它减慢了过程,最后您会明白):

import time

def main2():

AMS_DESTINATION = "Schiphol, Amsterdam"
GLA_DESTINATION = "Glasgow, Scotland"
AMS_PRICE = 150.00
GLA_PRICE = 80.00

# User input [LLL 0 00 L L]
# flightSpecification = str(input("Enter Flight Specification: "))
flightSpecification = 'AMS 1 22 V F'
flightDestination = flightSpecification[0:3]

bagCount = int(flightSpecification[4])
baggageCost = float((bagCount - 1) * 20)

passengerAge = int(flightSpecification[6:8])

standardMeal = 10.00
vegetarianMeal = 12.00
mealType = str(flightSpecification[9])

seatClass = str(flightSpecification[11])

totalFlightCost = 0

if flightDestination.lower() != 'ams' and flightDestination.lower() != 'gla':
print("Please enter a valid flight specification! [LLL 0 00 L L]")
time.sleep(2)
main()

if flightDestination.lower() == 'ams':
print(f"Destination: {AMS_DESTINATION}")
print(f"Flight cost: £{AMS_PRICE}")
totalFlightCost = 150

elif flightDestination.lower() == 'gla':
print(f"Destination: {GLA_DESTINATION}")
print(f"Flight cost: £{GLA_PRICE}")
totalFlightCost = 80

print(f"Number of bags: {bagCount}")
print(f"Baggage Cost: £{baggageCost}")

if passengerAge > 15:
print("Child: False")

elif passengerAge <= 15:
print("Child: True")
totalFlightCost = totalFlightCost / 2
standardMeal = standardMeal - 2.50
vegetarianMeal = vegetarianMeal - 2.50

elif passengerAge < 0:
print("Age cannot be negative")
main()

if mealType == 'S' and seatClass != 'F':
totalFlightCost = totalFlightCost + standardMeal
print("Meal: Standard")
print(f"Meal Cost: £{standardMeal}")

elif mealType == 'V' and seatClass != 'F':
totalFlightCost = totalFlightCost + vegetarianMeal
print("Meal: Vegetarian")
print(f"Meal Cost: £{vegetarianMeal}")

elif mealType == 'N':
print("Meal: None")
print("Meal Cost: £0")

# THIS COULD DEFINITELY BE DONE MORE EFFICIENTLY
if seatClass == 'F' and mealType is 'S':
totalFlightCost = totalFlightCost + standardMeal
print("Meal: Standard")
print(f"Meal Cost: FREE")

print("Seating Class: First")
totalFlightCost = totalFlightCost * 6

elif seatClass is 'F' and mealType == 'V':
totalFlightCost = totalFlightCost + vegetarianMeal
print("Meal: Vegetarian")
print(f"Meal Cost: £{vegetarianMeal}")

print("Seating Class: First")
totalFlightCost = totalFlightCost * 6

elif seatClass == 'E':
print("Seating Class: Economy")

print(totalFlightCost)
测试
更改 main 中的输入,您将在下面的测试中认识到没有真正的性能问题:
start = time.time()
main()
print('main() takes: ')
print(time.time() - start)

start = time.time()
main2()
print('main2() takes:')
print(time.time() - start)
结果
main() 需要:
7.319450378417969e-05 秒
main2() 需要:
4.935264587402344e-05 秒
值得一提
在您设置的递归路径中(在评论中提到),您还使用 time.sleep(2)。插入硬编码 sleep 总是会影响你的表现。

关于python - 如何使 IF 语句更有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64498933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com