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

Python while循环中匹配C选项的elif分支失效问题求助

问题解决:C选项分支失效的原因及修复

问题描述

在这段沙漠生存游戏代码中,所有if/elif分支均能正常运行,唯独elif user_choice.upper() == "C"(全速前进)的分支无响应。输入选项C后,程序未执行对应逻辑(打印行驶里程、更新数值),而是直接回到输入选择的步骤。

错误原因

问题出在C选项的判断语句上:

elif user_choice.upper == "C":

此处调用字符串的upper()方法时遗漏了括号,user_choice.upper是引用方法本身,而非执行方法得到大写后的字符串。该条件永远无法成立,程序会跳过此分支,后续也未匹配到用户输入相关的分支,最终直接进入下一次循环,重新要求输入。

修复方法

将C选项的判断语句修改为:

elif user_choice.upper() == "C":

添加upper()的括号,确保执行方法并得到大写后的用户输入,即可正确匹配选项C并执行对应逻辑。

修复后的完整代码

# Game info
print("Welcome to camel")
print("You have stolen a camel too make your way across the great Mobi desert")
print("The natives are chasing you and want their camel back. Survive and run out the natives")

# Storing values in variables
miles_travelled = 0
drinks_canteen = 3
natives_travelled = -20
camel_tiredness = 0
players_thirst = 0
done = False

# Making the loop
while not done:
    print("A. Drink from your canteen")
    print("B. Ahead moderate speed")
    print("C. Ahead full speed")
    print("D. Stop for the night")
    print("E. Status check")
    print("F. Quit")
    user_choice = input("Your choice? ")
    if user_choice.upper() == "A":
        if drinks_canteen == 0:
            print("No drinks in your canteen")
            user_choice = input("Your choice? ")
        elif drinks_canteen != 0:
            drinks_canteen -= 1
    elif user_choice.upper() == "B":
        miles_travelled += 7
        print("Miles travelled:", miles_travelled)
        players_thirst += 1
        camel_tiredness += 1
        natives_travelled += 9
    elif user_choice.upper() == "C":
        miles_travelled += 15
        print("Miles travelled:", miles_travelled)
        players_thirst += 1
        camel_tiredness += 3
        natives_travelled += 9
    elif user_choice.upper() == "D":
        camel_tiredness = 0
        print("The camel is happy")
        natives_travelled += 9
    elif user_choice.upper() == "E":
        print("Miles travelled:", miles_travelled)
        print("Drinks in canteen:", drinks_canteen)
        print("The natives are", natives_travelled, "behind you")
    elif user_choice.upper() == "F":
        print("Game Ends")
        done = True
    elif 4 < players_thirst < 6:
        print("You are thirsty")
    elif players_thirst > 6:
        print("You died of thirst")
        done = True
    elif 5 < camel_tiredness < 8:
        print("Your camel is getting tired")
    elif camel_tiredness > 8:
        print("Your camel died")
        done = True
    elif miles_travelled == natives_travelled:
        print("The natives caught up")
        done = True
    elif miles_travelled > 200:
        print("You won")
        done = True
    elif miles_travelled == natives_travelled + 15:
        print("The natives are getting close")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 03:50:45