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而不是程序结束时会发生什么 现在我只是添加详细信息,因此我会发布这个,因为我在下面解释了大部分内容
答案 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)
让我们稍微清理一下循环结构:
...
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'
你仍然需要将它调整到你的高级游戏控制,插入你的其他两个例程,并获得一些可用的变量名称,但这应该让你开始。