Python调试旅行计划

2024-09-24 06:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在想我到底做错了什么,只是不明白。Python说第22行有语法错误,但我不知道它有什么问题。。。请帮忙。这是另一个程序的一部分,但这个程序有问题。在

# 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

Tags: andin程序returndefareprintrates
1条回答
网友
1楼 · 发布于 2024-09-24 06:29:00

您的代码中有几个错误。请检查此正确版本和我在代码中的注释:

#!/usr/bin/python
# -*- coding: utf-8 -*-
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:
            # here you need a right bracket
            choice = int(input("Where would you like to go? "))
            # here you can put each condition into its own block
            if (choice < 1) or (choice > 3):
                #here you need some identation
                print("Please select a choice between 1 and 3.")
                continue
        except ValueError:
            # here you have to put the whole string into ""
            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
    # here you have to add a colon after every if statement
    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

相关问题 更多 >