You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python电影票预订程序运行报错,请求帮忙定位代码问题

Python电影票预订项目的代码错误分析与修复

存在的错误点:

  • 全局变量未初始化:t_movie函数中使用全局变量f,但未在全局作用域初始化,首次调用会触发NameError。
  • 缩进错误:t_movie函数内f = f+1行的缩进与上一行global f不一致,Python要求函数内代码缩进统一,会触发IndentationError。
  • 流程逻辑混乱:在t_movie中选择4(返回)时,代码先调用center()再调用theater(),但center()执行完会重新进入t_movie流程,导致后续theater()调用脱离上下文,属于逻辑错误。
  • 输入异常未处理:多处使用int(input())转换用户输入,若输入非整数会触发ValueError;timing函数直接通过键取值,输入非1-4选项会触发KeyError。
  • 命名冲突:movie函数的参数名theater与后续theater函数同名,易造成代码混淆。

修复后的完整代码:

# 初始化全局变量f
f = 0

# 选择电影名称的函数
def t_movie():
    global f
    f = f + 1
    print("你想看哪部电影?")
    print("1, 电影1 ")
    print("2, 电影2 ")
    print("3, 电影3")
    print("4, 返回")
    try:
        movie_choice = int(input("请选择电影:"))
        if movie_choice == 4:
            # 返回至上一级影院选择
            center()
            return
        elif 1 <= movie_choice <=3:
            print(f"已选择电影{movie_choice}")
            theater()
        else:
            print("无效选项,请重新选择")
            t_movie()
    except ValueError:
        print("请输入有效数字")
        t_movie()

# 选择放映厅的函数
def theater():
    print("你想选择哪个放映厅?")
    print("1, 放映厅1")
    print("2, 放映厅2")
    print("3, 放映厅3")
    try:
        screen_choice = int(input("请选择放映厅:"))
        if not 1 <= screen_choice <=3:
            print("无效选项,请重新选择")
            theater()
            return
        ticket_num = int(input("请输入购票数量:"))
        if ticket_num <=0:
            print("购票数量不能为0或负数")
            theater()
            return
        print(f"已选择{ticket_num}张票")
        timing(screen_choice)
    except ValueError:
        print("请输入有效数字")
        theater()

# 选择放映时间的函数
def timing(a):
    time1 = {
        "1": "10:00-13:00",
        "2": "13:10-16:10",
        "3": "16:20-19:20",
        "4": "19:30-22:30"
    }

    time2 = {
        "1": "10:15-13:15",
        "2": "13:25-16:25",
        "3": "16:35-19:35",
        "4": "19:45-22:45"
    }

    time3 = {
        "1": "10:30-13:30",
        "2": "13:40-16:40",
        "3": "16:50-19:50",
        "4": "20:00-22:45"
    }

    time_maps = {1: time1, 2: time2, 3: time3}
    print("请选择放映时间:")
    print(time_maps[a])
    try:
        t = input("请选择时间序号:")
        x = time_maps[a][t]
        print(f"预订成功!请于{x}观影")
    except KeyError:
        print("无效的时间选项,请重新选择")
        timing(a)

def movie(center_choice):
    if 1 <= center_choice <=3:
        t_movie()
    elif center_choice == 4:
        city()
    else:
        print("无效选项,请重新选择")
        center()

def center():
    print("你想选择哪家影院?")
    print("1, Inox")
    print("2, Icon")
    print("3, pvp")
    print("4, 返回")
    try:
        a = int(input("请选择选项:"))
        movie(a)
    except ValueError:
        print("请输入有效数字")
        center()

# 选择城市的函数
def city():
    print("欢迎来到电影票预订系统!")
    print("你想在哪个城市观影?")
    print("1, 城市1")
    print("2, 城市2 ")
    print("3, 城市3 ")
    try:
        place = int(input("请选择选项:"))
        if 1 <= place <=3:
            print(f"已选择城市{place}")
            center()
        else:
            print("无效选项,请重新选择")
            city()
    except ValueError:
        print("请输入有效数字")
        city()

# 启动程序
city()

内容的提问来源于stack exchange,提问作者Aditya Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:35:17