Python-需要根据用户输入来计算成本

时间:2019-09-21 19:05:31

标签: python arrays input calculator

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

您知道我可能会缺少什么,以及是否可以使用更好的阵列吗?

#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")

1 个答案:

答案 0 :(得分:0)

在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基础。