首页 > 解决方案 > Python-需要根据用户输入计算成本

问题描述

我需要根据用户输入和使用数组来计算成本。我有以下代码,但不断收到错误(指向我的“打印”括号)。

你知道我可能会错过什么,以及是否可以使用更好的数组吗?

#route type
yourUserInput = input("will you use route 1 or 2 ")

finance = 1 #
if yourUserInput == "1":
    finance = 25
elif yourUserInput == "2":
    finance = 35
else:
    print("you did not enter a valid route")

print ("total cost" (cost))
# ticket type
tickettype = input("what type of ticket would you like (single or return) ")
price = 1 #
if tickettype == "single" or tickettype == "Single":
    price = 25
elif tickettype == "return" or tickettype == "Return":
    price = 35
else:
    print("you did not enter a valid ticket type")

#cost = int( finance ) *int( price )


ar= (finance + price)
#print "the total is therefore",
print ("your total price is" int(ar))

input("press enter to exit the program")

标签: pythonarraysinputcalculator

解决方案


在 Python 中,当您想在输出中包含多个变量或文本时,可以使用逗号 (,) 将其添加到输出语句中。它类似于在 Java 中在输出语句中添加 + 的方式。

print ("total cost" (cost)) 应该是 print ("total cost", cost) 而且 print ("your total price is" int(ar)) 应该是 print ("your total price is", int(ar))

在您的代码中实现一个数组看起来像这样,其中数组中的两个值是 25 和 35。

yourUserInput = input("will you use route 1 or 2 ")

cost = 1
finance = 1
price = 1
list_values = [25,35]

if yourUserInput == "1":
    finance = list_values[0]
elif yourUserInput == "2":
    finance = list_values[1]
else:
    print("you did not enter a valid route")

print ("total cost = ", cost)
# ticket type
tickettype = input("what type of ticket would you like (single or return) ")

if tickettype == "single" or tickettype == "Single":
    price = list_values[0]
elif tickettype == "return" or tickettype == "Return":
    price = list_values[1]
else:
    print("you did not enter a valid ticket type")

cost = finance * price
ar = (finance + price)

#print "the total is therefore",
print ("your total price is", ar)

input("press enter to exit the program")

我鼓励您阅读https://docs.python.org/3/whatsnew/3.0.html以更好地了解 Python 基础知识。


推荐阅读