Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python-猜游戏,确保用户不';同一个字母不能输入两次_Python 2.7 - Fatal编程技术网

Python 2.7 Python-猜游戏,确保用户不';同一个字母不能输入两次

Python 2.7 Python-猜游戏,确保用户不';同一个字母不能输入两次,python-2.7,Python 2.7,所以基本上这就是我所拥有的 print "***Welcome to Code Breaker***" print "\n" rounds = raw_input("How many rounds do you want to play (has to be a positive integer)? ") while rounds.isdigit() == False or int(rounds) < 1: rounds = raw_input("ERROR:How

所以基本上这就是我所拥有的

print "***Welcome to Code Breaker***"

print "\n"

rounds = raw_input("How many rounds do you want to play (has to be a positive integer)? ")

while rounds.isdigit() == False or int(rounds) < 1:    
    rounds = raw_input("ERROR:How many turns do you want to play (has to be a positve integer)? ")

print "\n"

i = 0

while i < int(rounds):
    i = i + 1
    for i2 in range(2):
        if i2 == 0:
            player = 1
            breaker = 2
        else:
            player = 2
            breaker = 1

    print "Round" + str(i) + ":***Player " + str(player) + "'s turn to setup the game.***"

    print "Player " + str(breaker) + " look away PLEASE!"

    secret = raw_input("Type in the secret word, QUICKLY? ")

    while secret.isalpha() == False:
        secret = raw_input("ERROR: Type in the secret word (has to be letters): ")

    secret = secret.lower()
    print "\n"*100


    numberOfGuess = raw_input("How many guesses will you allow?(has to be a positive integer) ")

    while numberOfGuess.isdigit() == False or int(numberOfGuess) < 1:
        numberOfGuess = raw_input("ERROR:How many guesses will you allow? (has to be a positive integer) ")

    def maskWord(state, word, guess):
        state = list(state)
        for i in range(len(word)):
            if word[i] == guess:
                state[i] = guess
        return "".join(state)


    word = secret
    state = "*" * len(word)
    tries = 0
    print "Secret Word = " + state
    play = True

    while play:
        if tries == int(numberOfGuess): 
            print "Fail...";
            break
            play = False
        guess = raw_input("Guess a letter (a-z)? ")

        while guess.isalpha() == False or len(guess)!= 1:
            guess = raw_input("ERROR: Guess a letter (a-z)? ")

        guess = guess.lower()
        tries +=1
        state = maskWord(state, word, guess)
        print state
        if maskWord(state, word, guess) == word:  
            print "WIN, WIN!!"; 
            play = False


    print "\n" * 100
print“***欢迎来到破译程序***”
打印“\n”
轮数=原始输入(“您想玩多少轮(必须是正整数)?”)
而rounds.isdigit()==False或int(rounds)<1:
轮数=原始输入(“错误:您想玩多少圈(必须是正整数)?”)
打印“\n”
i=0
当i

问题:在代码的猜测部分,我想设置它,因为用户不能猜同一个字母两次。我知道您必须使用空列表并使用.append函数来存储数据。然而,我尝试过,在许多不同的方面,它似乎都不起作用。我不知道我哪里做错了,如果有人能回答这个问题,那就太好了。我需要知道它会是什么样子,以及应该将它放在代码中的什么位置。谢谢

我没有阅读所有的代码,但看看你的问题,我认为你在寻找类似的东西:

l = []

#build list

char = 'a'

if char in l:
    print('error')
else:
    l.append(char)

这是典型的使用一套跟踪这样的事情

used_letters = set()  # a new empty set
# ...
if guess in used_letters:  # test presence
  print "This letter has been used already!"
else:
  used_letters.add(guess) # remember as used
您可以使用
list()
.append(guess)
代替。有了列表,效率会降低,但在您的情况下,效率低下是完全无法检测到的


使用
集合
的目的是传达这样一种理念,即不应该存在重复的字母。(你知道,程序读起来比写起来要频繁得多。)

谢谢你的帮助人=)尽管我不理解char='a'部分,但这只是一个示例,因此在你的示例中,它将是用户的输入。;)