创建旅行计划器,尝试调试我的代码

时间:2015-03-06 03:42:41

标签: python debugging

这是我的代码,我已经知道我的目的地和货币代码是正确的。但是我不确定这个代码,因为每次我尝试运行它时,它都不起作用。我一直被告知语法错误,但我需要再看看这里出了什么问题,因为我无法理解自己。我已经完成了它并且我试图解决我看错的事情。请帮忙。

# 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

1 个答案:

答案 0 :(得分:1)

根据您的评论,您表示从

行收到语法错误
convert_dollars_to_euros(dollar_rate):
    return dollar_rate / 1.12

以下内容使其成为合法的函数声明:

def convert_dollars_to_euros(dollar_rate):
    return dollar_rate / 1.12

您错过了关键字&#34; def&#34;,这是制作函数时所必需的,这是您自定义函数&#34; main&#34;在你的其他代码中。

另外,虽然它会编译,

import destinations.py

也不正确,因为它会查找名为&#34; py&#34;的对象。在空间&#34;目的地&#34;

import destinations

工作得很好。它的编写方式,您将获得运行时异常,类似于ImportError