在Python的杯子比赛下随机球

时间:2015-09-30 22:34:27

标签: python

def talon_game():
mylist = ['1', '2', '3'] 
keep_playing = True

while keep_playing:

    print "\nThere are 3 cups, and 1 egg hidden underneath."
    print "Which cup do you think the egg is under?\n"

    talong_c = raw_input("1 - 2 - 3? ")

    while talong_c not in "123":

        talong_c = raw_input("1 - 2 - 3? ")

    true_cup = random.choice(mylist)
    if talong_c == true_cup:
        print "\nYou win!"
        print "Would you like to play again?\n"
        talong_c1 = raw_input("> ")
        keep_playing = 'yes' in talong_c1.lower()
    elif talong_c1 != 'yes': #added this as one example, but nothing has worked obviously
        keep_playing = False
    else:
        print "\nYou lose!  It was under cup", true_cup
        print "Would you like to play again?\n"
        talong_c1 = raw_input("> ")
        keep_playing = 'yes' in talong_c1.lower()

#注释的附加编码 不确定当keep_playing为False而不是程序结束时会发生什么 现在我只是添加详细信息,因此我会发布这个,因为我在下面解释了大部分内容

2 个答案:

答案 0 :(得分:1)

你的问题到底是什么?

关于random.choice(),也许你需要这样的东西:

mylist = ['1', '2', '3']   # Since input will be a string, not an int.
answer = random.choice(mylist)

后面跟着

if talong_c == answer:
    # ...

答案 1 :(得分:0)

让我们稍微清理一下循环结构:

  1. 当您需要输入时,设置一个持续到您的while循环 得到它。
  2. 当你想继续玩游戏的时候,就像循环一样 只要玩家进入某种形式的“是”。
  3. 递归会在运行时堆栈上产生错误的业力。
  4. 简化输入检查。
  5. ...

    import random
    
    mylist = [1, 2, 3]
    keep_playing = True
    
    while keep_playing:
    
        # Get legal guess
        print "There are 3 cups, and 1 egg hidden underneath."
        print "Which cup do you think the egg is under?"
        talong_c = raw_input("1 - 2 - 3? ")
        while talong_c not in "123":
            talong_c = raw_input("1 - 2 - 3? ")
    
        true_cup = random.choice(mylist)
        if int(talong_c) == true_cup:
            print "\nYou win!"
        else:
            print "\nYou lose!  It was under cup", true_cup
    
        print "Would you like to play again?"
        talong_c1 = raw_input("> ")
        keep_playing = talong_c1.lower() == 'y'
    

    你仍然需要将它调整到你的高级游戏控制,插入你的其他两个例程,并获得一些可用的变量名称,但这应该让你开始。