这是我的代码,我已经知道我的目的地和货币代码是正确的。但是我不确定这段代码,因为每次我尝试运行它时,它都不起作用。我不断被告知语法错误,但我需要再看一眼看看这里出了什么问题,因为我自己不太明白。我已经经历了它,我正在尝试解决我认为错误的事情。请帮忙。
# Trip Planner
# ------------
# The following program helps to create a travel itinerary
# Import modules
import destinations.py
import currency.py
def main():
# Print a welcome message
print_welcome()
# Show destinations
destinations.print_options()
# Pick destination
choice = destinations.get_choice()
# Get destination info
destination = destinations.get_info(choice)
# Calculate currency exchange
dollar_rate = currency.convert_dollars_to_euros(euro_rate)
# Determine length of stay
while True:
try:
length_of_stay = int(input("And how many days will you be staying in," destination " ?" ))
# Check for non-positive input
if (length_of_stay < 0):
print("Please enter a positive number of days.")
continue
except ValueError:
print("The value you entered is invalid. Only numerical values are valid.")
else:
break
# Calculate cost
cost = dollar_rate + length_of_stay
# Save itinerary
try:
save_itinerary(destination, length_of_stay, cost)
# Catch file errors
except:
print("Error: the itinerary could not be saved.")
# Print confirmation
else:
print("Your trip to", destination "has been booked!")
# Call main
main()
def print_welcome():
# Print a welcome message
print("---------------------------")
print("Welcome to the Trip Planner")
print("---------------------------")
def save_itinerary(destination, length_of_stay, cost):
# Itinerary File Name
file_name = "itinerary.txt"
# Create a new file
itinerary_file = open(file_name, "r")
# Write trip information
file_name.write("Trip Itinerary")
file_name.write("--------------")
file_name.write("Destination: " + destination)
file_name.write("Length of stay: " + length_of_stay)
file_name.write("Cost: $" + format(cost, ",.2f"))
# Close the file
file_name.close()
这是目标代码:
# Destinations Module
# -------------------
# This module provides information about European destinations and rates
# All rates are in euros
def print_options():
# Print travel options
print("Travel Options")
print("--------------")
print("1. Rome")
print("2. Berlin")
print("3. Vienna")
print("")
def get_choice():
# Get destination choice
while True:
try:
choice = int(input("Where would you like to go? "))
if (choice < 1) or (choice > 3):
print("Please select a choice between 1 and 3.")
continue
except ValueError:
print("The value you entered is invalid. Only numerical values are valid.")
else:
return choice
def get_info(choice):
# Use numeric choice to look up destination info
# Rates are listed in euros per day
# Choice 1: Rome at €45/day
if (choice == 1):
return "Rome", 45
# Choice 2: Berlin at €18/day
elif (choice == 2):
return "Berlin", 18
# Choice 3: Vienna, €34/day
elif (choice == 3):
return "Vienna", 34
这是货币代码:
# Currency Module
# ---------------
# This module is used to convert between different types of currency.
convert_dollars_to_euros(dollar_rate):
return dollar_rate / 1.12
convert_euros_to_dollars(euro_rate):
return euro_rate * 1.12
从您的评论中,您指出您从该行中得到语法错误
convert_dollars_to_euros(dollar_rate):
return dollar_rate / 1.12
以下使这成为法律功能声明:
def convert_dollars_to_euros(dollar_rate):
return dollar_rate / 1.12
您缺少关键字"def",这是创建函数时必需的关键字,自从您在其他代码中定义了函数"main"以来,您似乎已经知道这一点。
另外,虽然它会编译,
import destinations.py
也不正确,因为它将在空间"目的地"中查找名为"py"的对象
import destinations
工作得很好。它的编写方式,你会得到一个运行时异常,类似于 ImportError