如何循环设定的次数

时间:2015-02-28 20:44:30

标签: list python-3.x while-loop counter

我正在编写一个模拟游戏单词拼字游戏的代码。我试图做的是它要求用户解读这个词,如果他们失败了,一旦他们获得了另一个机会,等等3个机会。在3次机会之后,程序应该告诉他们他们无法在机会限制内猜测它,程序应该告诉他们这个词。

rand_artist = artist_names[random.randrange(len(artist_names))]
tries = 0
while tries < 3:
    rand_input = enterbox("Unscrabble the following: {}".
            format(txt), "Word Scrabble")
    if rand_input != rand_artist:
        msgbox("Try again!", "Word Scrabble")
        tries +=1
    elif rand_input == rand_artist:
        msgbox("Congratulations! You guessed the word!")
        tries +=3
    elif tries > 2: 
        msgbox("You used up three chances! The word was {}".
                format(txt), "Word Scrabble!")

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题,有些问题在我对此问题的评论中有所说明。以下工作正如您所希望的那样。

from random import choice, shuffle
artist_names = ['Renoir', 'VanGogh', 'Rembrant', 'Homer', 'Pyle',]
artist = choice(artist_names)
alist = list(artist)
shuffle(alist)
scram = ''.join(alist)

for tries in range(1, 4):
    guess = input("Unscrabble {}: ".format(scram))
    if guess == artist:
        print("Congratulations! You guessed the word!")
        break
    elif tries < 3:
        print("Try again!")
    else:
        print("Failed three chances! The word was {}.".format(artist))