如何找到这个python程序的总成本

js5cn81o  于 2023-01-12  发布在  Python
关注(0)|答案(3)|浏览(141)

我已经创建了一个简单的python程序,让用户驾驶不同的汽车。用户将输入他们的全名,地址和电话号码。然后用户将被询问他们希望驾驶哪辆车,最多选择五辆车。这些车已经设定了价格,总账单将在程序结束时加起来,然而,我目前无法找到解决方案来计算用户的总成本。程序还询问用户希望在车内进行多少圈比赛,我已经计算出如何显示圈数的总成本,但是需要以某种方式把它加到总成本中。谢谢!

    • 代码**
cars_list = ["Lamborghini", "Ferrari", "Porsche", "Audi", "BMW"]
cars_prices = {"Lamborghini":50, "Ferrari":45, "Porsche":45, "Audi":30, "BMW":30}
laps_cost = 30
final_cost = []
final_order = {}

cust_num_cars = int(input("Please enter the number of cars you want to drive in this session: "))
while cust_num_cars > 5:
    cust_num_cars = int(input("You can only drive a maximum of five cars! Please try again.\n Enter cars: "))

for index in range(cust_num_cars):
    print("\nChoose a car type from the following list", cars_list)
    select_cars = input("Select a car type: ")
    if select_cars in cars_list:
        print("\nYou have selected to drive the", {select_cars})
        final_cost.append(cars_prices[select_cars])
    if select_cars not in cars_list:
        print("\n",{select_cars}, "is not in the available list of cars to drive!")

cust_name = input("\nPlease enter your full name: ")
cust_address = input("Please enter your full address: ")
cust_phone_num = int(input("Please enter your mobile phone number: "))

add_laps = input("\nWould you like to drive additional laps? (Yes/No): ")
if add_laps == "Yes":
    print("\nYou have selected an additional lap!")
    num_of_laps = int(input("Number of additional laps: "))
    print("\nYou have selected", num_of_laps, "additional laps!")
    final_cost.append(cars_prices[add_laps])
    sum = num_of_laps * laps_cost
else:
    print("\nYou have selected no additional extra laps.")

print("Total laps cost: £",final_cost)
print("\n----Order & Billing summary----")
print("Customer Full Name:", cust_name)
print("Customer Address:", cust_address)
print("Customer Phone Number", cust_phone_num)
print("Total cost", final_cost.append(cars_prices))

我已经尝试了我在Python中的所有方法来计算最终成本,我已经计算出了圈数的总成本,但无法计算出如何将其添加到所选汽车的成本中,然后显示总成本。

wpx232ag

wpx232ag1#

首先,不能使用for循环,因为当您选择一辆不在可能性范围内的汽车时,您已经丢失了一次迭代,请使用while循环

# final_cost renamed to car_cost
while len(car_cost) != cust_num_cars:
    print("\nChoose a car type from the following list", cars_list)
    select_cars = input("Select a car type: ")
    if select_cars in cars_list:
        print("\nYou have selected to drive the", select_cars)
        car_cost.append(cars_prices[select_cars])
    else:
        print("\n", select_cars, "is not in the available list of cars to drive!")

然后使用sum()作为汽车成本,并添加到圈数成本中

num_of_laps = 0
add_laps = input("\nWould you like to drive additional laps? (Yes/No): ")
if add_laps == "Yes":
    print("\nYou have selected an additional lap!")
    num_of_laps = int(input("Number of additional laps: "))
    print("\nYou have selected", num_of_laps, "additional laps!")
else:
    print("\nYou have selected no additional extra laps.")

total = num_of_laps * laps_cost + sum(car_cost)
print("Total price:", total)
lmvvr0a8

lmvvr0a82#

要计算总成本,可以使用内置的sum()函数,该函数接受一个可迭代对象(如列表),并返回其所有元素的总和,在本例中,可以使用该函数将用户选择的所有汽车的价格相加:

total_cost = sum(final_cost)

然后你可以把圈数的成本加到总成本中。在你的代码中,变量sum保存圈数的总成本。你可以把它加到total_cost变量中:

total_cost += sum
thigvfpy

thigvfpy3#

嗨,朋友,在这个程序中,最好把你所有的成本加在一个变量中。在列表中,你的列表元素不加在一起,但与变量,你可以添加你的成本,每次你得到这样的成本:

cost_sum = 0 
cost = input("Enter a cost: ")
cost_sum += cost

相关问题