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

第18行语法错误致银行账户菜单程序无法执行,请求调试帮助

代码调试解决方案

存在的语法及逻辑错误

  1. 第18行条件判断逻辑错误:

    • 原写法if choice == "Q" or "q" or "1" or "2" or "3"不成立,or连接的每个条件都需要完整比较(如choice == "q"),否则非空字符串会被判定为True,导致循环直接终止。
    • 逻辑完全搞反:应该是输入不在合法选项时提示错误,而非退出循环;只有输入Q/q时才触发退出。
  2. 语法缺失错误:

    • 第21行deposit = int(input("Enter an amount to deposit: GBP"))末尾缺少右括号)
    • 第29行withdraw = int(input("Enter an amount to withdraw: GBP"))末尾缺少右括号)
  3. 结构错误:

    • 原代码将操作处理的if-elif块放在while循环外部,导致只能执行一次操作,不符合“多次操作”需求,需将其移入循环内部。

修正后的完整代码

print("*******WELCOME TO OUR BANK*******")

user_name = input("请输入您的姓名: ")

balance = 1000

while True:
    print("选择 1 进行存款")
    print("选择 2 进行取款")
    print("选择 3 查询余额")
    print("输入 Q 或 q 退出")
    choice = input("请输入您的选项: ")
    
    # 校验输入合法性
    if choice not in ("Q", "q", "1", "2", "3"):
        print("输入错误! 请重新输入。")
        continue
    
    # 处理退出逻辑
    if choice in ("Q", "q"):
        print("感谢您选择我们的银行")
        print("欢迎再次光临!")
        break
    
    # 存款操作
    if choice == "1":
        deposit = int(input("请输入存款金额: GBP"))
        balance += deposit
        print("存款已完成,账户余额已更新。")
        print(f"{user_name} 的当前账户余额为: GBP {balance}")
    
    # 取款操作
    elif choice == "2":
        withdraw = int(input("请输入取款金额: GBP"))
        if withdraw > balance:
            print("无法提取超过账户余额的金额")
            print("请重试!")
        else:
            balance -= withdraw
            print("取款已完成,账户余额已更新。")
            print(f"{user_name} 的当前账户余额为: GBP {balance}")
    
    # 查询余额操作
    elif choice == "3":
        print(f"{user_name} 的当前账户余额为: GBP {balance}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 07:50:30