Python问题中的角色创建者

时间:2016-02-14 20:07:03

标签: python python-3.2

我在解决问题时遇到严重问题似乎是一个非常基本的问题而且我不知道为什么它不会起作用。

基本上,我是为基于文本的游戏制作角色创建者,而且您需要选择三种角色类型之一。但是,如果我输入正确或不正确的字符类型,它仍然可以让我再次输入它。

我正在使用python 3.2

def choose_mech_type():
    mecha_type = input("""
Choose mecha type:
Marine
Flight
Ground
""")

    if mecha_type == ("marine"):
        head = ("m.head.basic")
        body = ("m.body.basic")
        back = ("m.back.basic")
        arms = ("m.arms.basic")
        hands = ("m.hands.basic")
        legs = ("m.head.basic")
        feet = ("m.feet.basic")

    if mecha_type == ("flight"):
        head = ("f.head.basic")
        body = ("f.body.basic")
        back = ("f.back.basic")
        arms = ("f.arms.basic")
        hands = ("f.hands.basic")
        legs = ("f.head.basic")
        feet = ("f.feet.basic")

    if mecha_type == ("ground"):
        head = ("g.head.basic")
        body = ("g.body.basic")
        back = ("g.back.basic")
        arms = ("g.arms.basic")
        hands = ("g.hands.basic")
        legs = ("g.head.basic")
        feet = ("g.feet.basic")

    if mecha_type != ("ground") or ("marine") or ("flight"):
        choose_mech_type()

1 个答案:

答案 0 :(得分:0)

代码的主要问题是以下行始终为真:

if mecha_type != ("ground") or ("marine") or ("flight"):

检查此逻辑的正确方法是:

if mecha_type not in ["ground", "marine", "flight"]:

另一个主要问题是,一旦你进入if语句,你就递归调用该函数。而不是这样做,你应该使用一个循环,当它有有效的输入时退出。

def choose_mech_type():
    while True:     
        mecha_type = input("""
Choose mecha type:
Marine
Flight
Ground
""")
        if mecha_type in ["ground", "marine", "flight"]:
            return mecha_type