if语句未被识别[在game()函数中]

时间:2016-07-30 20:00:11

标签: python-2.7

import time
import random
import sys

tries = 1

def start():
    global tries
    tries = 1
    global chest1
    chest1 = random.sample(xrange(1, 20), 1)
    chest1==str
    global chest2
    chest2 = random.sample(xrange(1, 20), 1)
    chest2==str
    global chest3
    chest3 = random.sample(xrange(1, 20), 1)
    chest3==str
    print chest1
    while chest1 == chest2 or chest1 == chest3 or chest2 == chest3:
        start()
    else:
        print"Alright lets begin!"
        game()

def defeat():
    time.sleep(3)
    end = raw_input("Would you like to start again(yes or no)?")
    if end == "yes":
        start()
    if end == "no":
        print"Goodbye!"
        time.sleep(1);
        print"Shutting Down"
        time.sleep(2)
        sys.exit()
    else:
        print"Please input a valid answer"   

def game():
    global chest1
    global chest2
    global chest3
    print chest1, chest2, chest3
    num = input("Choose a chest from 1-20!")
    if num == chest1 or num == chest2 or num == chest3:
        print "Well Done! Get another chest to move on to Sudden Death!"
    else:
        while (tries < 3):
            global tries
            print"Try Again"
            tries = tries + 1
            game()
        else:
            print "You've taken too many tries.. YOU DIE!"
            defeat()

运行此代码时,我到达if语句:

if num == chest1 or num == chest2 or num == chest3:
    print "Well Done! Get another chest to move on to Sudden Death!"

并将其中一个箱子与我的输入相匹配(我知道自从我打印它们后选择的数字)它跳转到else语句并说“#34;再试一次”#34;即使我将正确的输入与胸部相匹配。我已经在这几个小时......请帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

至少有几个问题:

  1. 您要为chest1chest2chest3分配一个列表。
  2. 输入是一个字符串,您将其与具有int的单元素列表进行比较。
  3. 请改为尝试:

    import time
    import random
    import sys
    
    tries = 1
    
    def start():
        global tries
        tries = 1
        global chest1
        chest1 = random.randint(1, 20)
        global chest2
        chest2 = random.randint(1, 20)
        global chest3
        chest3 = random.randint(1, 20)
        while chest1 == chest2 or chest1 == chest3 or chest2 == chest3:
            start()
        else:
            print("Alright lets begin!")
            game()
    
    def defeat():
        time.sleep(3)
        end = raw_input("Would you like to start again(yes or no)?")
        if end == "yes":
            start()
        if end == "no":
            print("Goodbye!")
            time.sleep(1);
            print("Shutting Down")
            time.sleep(2)
            sys.exit()
        else:
            print("Please input a valid answer")
    
    def game():
        global chest1
        global chest2
        global chest3
        print(chest1, chest2, chest3)
        num = int(raw_input("Choose a chest from 1-20!"))
        if num == chest1 or num == chest2 or num == chest3:
            print("Well Done! Get another chest to move on to Sudden Death!")
        else:
            while (tries < 3):
                global tries
                print("Try Again")
                tries = tries + 1
                game()
            else:
                print("You've taken too many tries.. YOU DIE!")
                defeat()
    

    编辑:通常也不赞成使用全局变量。我冒昧地重构你的代码而不使用它们:

    import time, random, sys
    
    def start():
        chest1 = random.randint(1, 20)
        chest2 = random.randint(1, 20)
        chest3 = random.randint(1, 20)
        tries = 1
        while chest1 == chest2 or chest1 == chest3 or chest2 == chest3:
            start()
        else:
            print("Alright lets begin!")
            game(chest1,chest2,chest3,tries=tries)
        return tries 
    
    def defeat():
        time.sleep(3)
        end = raw_input("Would you like to start again(yes or no)?")
        if end == "yes":
            tries = start()
        if end == "no":
            print("Goodbye!")
            print("Shutting Down")
            sys.exit()
        else:
            print("Please input a valid answer")
    
    def game(chest1,chest2,chest3,tries=None):
        print(chest1, chest2, chest3)
        num = int(raw_input("Choose a chest from 1-20!"))
        if num == chest1 or num == chest2 or num == chest3:
            print("Well Done! Get another chest to move on to Sudden Death!")
            while (tries < 3):
                tries += 1
                game(chest1,chest2,chest3,tries=tries)
            else:
                print("Moving to Sudden Death!")
                sudden_death(chest1,chest2,chest3,tries=tries)
        else:
            while (tries < 3):
                print("Try Again")
                tries += 1
                game(chest1,chest2,chest3,tries=tries)
            else:
                print("You've taken too many tries.. YOU DIE!")
                defeat()
    
    def sudden_death(chest1,chest2,chest3,tries=None):
        print("Sudden Death not yet implemented; exiting")
        defeat()