Solenn
Solenn

Reputation: 47

Creating a trip planner, trying to debug my code

Here is the code I have, I already know that my destinations and currency codes are correct. However I am not sure about this code because every time I try to run it, it isn't working. I keep getting told syntax error, but I need a second look to see what is going wrong here because I can't quite understand myself. I have gone through it and I'm trying to fix things I see wrong. Please help.

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

Here is the destination code:

# 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

Here is the currency code:

# 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

Upvotes: 0

Views: 2897

Answers (1)

en_Knight
en_Knight

Reputation: 5381

From your comment, you indicated that you get a syntax error from the line

convert_dollars_to_euros(dollar_rate):
    return dollar_rate / 1.12

The following makes this a legal function declaration:

def convert_dollars_to_euros(dollar_rate):
    return dollar_rate / 1.12

You're missing the keyword "def", which is required when making functions, something you appear to already know since you defined the function "main" in your other code.

Aditionally, though it will compile,

import destinations.py

is not correct either, since it will look for an object named "py" in the space "destinations"

import destinations

works just fine. The way it's written, you'll get a runtime exception, something like an ImportError

Upvotes: 1

Related Questions