需要添加变量,而不是连接变量 Python
Need Variables to Add, Not Concatenate Python
这是我完成的编码:
from math import *
from time import sleep
def vacation():
print("Time to plan for a vacation.")
sleep(1)
dates = input("Let's start with the dates. What day are you getting there, and what day are you leaving? For formatting, you can use month/day-month/day.")
sleep(1)
location = input("Now where are you traveling to?")
sleep(1)
ticketCost = input("What is the cost of a two-way ticket to " + location + " and back?")
sleep(1)
dayOne = input("What is the cost for at least two meals on the first day?")
dayTwo = input("What is the cost for at least two meals on the second day?")
sleep(1)
hotel = input("What is the name of the hotel you are staying at?")
hotelCost = input("What is the cost for one night at (the) " + hotel + "?")
sleep(1)
print("Vacation! Going to " + location + " for " + dates + ".")
print("-------------------------------")
print("Total cost of the trip:")
print("-------------------------------")
sleep(1)
print("Plane Ticket: " + ticketCost)
sleep(1)
print("Estimate Cost of Meals (Day One): " + dayOne + ".")
sleep(1)
print("Estimate Cost of Meals (Day Two): " + dayTwo + ".")
sleep(1)
print("One Night at (the) " + hotel + ": " + hotelCost + ".")
sleep(1)
**total = hotelCost + dayOne + dayTwo + ticketCost**
**totalExtra = total + 50
print("The total is: " + total + ".")**
sleep(1)
print("Make sure you leave some room for souvenirs, pay tolls, and other expenses. Additional is added for this, and that total is: " + totalExtra + ".")
print("-------------------------------")
sleep(1)
print("Enjoy your vacation!")
vacation()
问题区域以粗体显示。我不知道该怎么做,我已经尝试在多个地方使用 int()、str() 等。总数不应该是乱码(即“843290842”)。我错过了什么吗?
input
函数returns一个字符串,不是一个整数。随处投射:
dates = int(input("Let's start with the dates. What day are you getting there, and what day are you leaving? For formatting, you can use month/day-month/day."))
等...
其他几点
如果您以后要将这些全部加在一起,请考虑将它们放入一个集合中,而不是将它们放在一堆松散的变量中。字典可以让您保留名称:
costs = {}
costs['dates'] = int(input("Let's start with the dates. What day are you getting there, and what day are you leaving? For formatting, you can use month/day-month/day."))
最后你可以很容易地通过字典循环得到总数
total = sum(costs.values())
请记住省略本应为 hotel
之类的字符串的值,这些值可能应该被称为 hotel_name
。
最后,您应该尝试字符串插值(这取决于您的 Python 版本,但对于 3.6+):
"What is the cost for one night at (the) " + hotel + "?"
变成
f"What is the cost for one night at (the) {hotel}?"
这是我完成的编码:
from math import *
from time import sleep
def vacation():
print("Time to plan for a vacation.")
sleep(1)
dates = input("Let's start with the dates. What day are you getting there, and what day are you leaving? For formatting, you can use month/day-month/day.")
sleep(1)
location = input("Now where are you traveling to?")
sleep(1)
ticketCost = input("What is the cost of a two-way ticket to " + location + " and back?")
sleep(1)
dayOne = input("What is the cost for at least two meals on the first day?")
dayTwo = input("What is the cost for at least two meals on the second day?")
sleep(1)
hotel = input("What is the name of the hotel you are staying at?")
hotelCost = input("What is the cost for one night at (the) " + hotel + "?")
sleep(1)
print("Vacation! Going to " + location + " for " + dates + ".")
print("-------------------------------")
print("Total cost of the trip:")
print("-------------------------------")
sleep(1)
print("Plane Ticket: " + ticketCost)
sleep(1)
print("Estimate Cost of Meals (Day One): " + dayOne + ".")
sleep(1)
print("Estimate Cost of Meals (Day Two): " + dayTwo + ".")
sleep(1)
print("One Night at (the) " + hotel + ": " + hotelCost + ".")
sleep(1)
**total = hotelCost + dayOne + dayTwo + ticketCost**
**totalExtra = total + 50
print("The total is: " + total + ".")**
sleep(1)
print("Make sure you leave some room for souvenirs, pay tolls, and other expenses. Additional is added for this, and that total is: " + totalExtra + ".")
print("-------------------------------")
sleep(1)
print("Enjoy your vacation!")
vacation()
问题区域以粗体显示。我不知道该怎么做,我已经尝试在多个地方使用 int()、str() 等。总数不应该是乱码(即“843290842”)。我错过了什么吗?
input
函数returns一个字符串,不是一个整数。随处投射:
dates = int(input("Let's start with the dates. What day are you getting there, and what day are you leaving? For formatting, you can use month/day-month/day."))
等...
其他几点
如果您以后要将这些全部加在一起,请考虑将它们放入一个集合中,而不是将它们放在一堆松散的变量中。字典可以让您保留名称:
costs = {}
costs['dates'] = int(input("Let's start with the dates. What day are you getting there, and what day are you leaving? For formatting, you can use month/day-month/day."))
最后你可以很容易地通过字典循环得到总数
total = sum(costs.values())
请记住省略本应为 hotel
之类的字符串的值,这些值可能应该被称为 hotel_name
。
最后,您应该尝试字符串插值(这取决于您的 Python 版本,但对于 3.6+):
"What is the cost for one night at (the) " + hotel + "?"
变成
f"What is the cost for one night at (the) {hotel}?"