python - 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")
解决方案
在 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 基础知识。
推荐阅读
- node.js - 每次我运行 gulp 或 gulp build local 我都会得到 AssertionError [ERR_ASSERTION]: Task never defined: build
- sql - 当给定每个科目的单独分数时,查找每个班级的最佳名单
- xamarin.android - 以编程方式设置包含 android:textColor 和 android:background 的样式?(Xamarin.Android)
- mockito - java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException 尝试使用 maven 运行 mockito junit5 测试
- angular - 角度 HttpClient 调用在 IE 中不起作用?
- javascript - Google Apps 脚本找不到已创建的广告系列
- python - 如何将结果 .json 文件保存到 IBM Cloud Storage?
- python - 使用 path.open('r', encoding="utf-8") 作为文件:AttributeError: 'generator' object has no attribute 'open'
- fortran - 在 Fortran 格式中使用双反斜杠
- reactjs - 在 _app.js (nextjs) 中接收新道具?