所以基本上就是我拥有的东西;
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
问题:在猜测代码部分我想设置它,因为用户不能两次猜出相同的字母。我知道你必须使用一个空列表并使用.append函数来存储数据。但是我已经尝试过,并且以许多不同的方式它似乎没有用。我不知道我在哪里做错了,如果有人能回答这个问题就会很棒。我需要知道它会是什么样的,我应该把它放在我的代码中。谢谢!
答案 0 :(得分:1)
我没有阅读所有代码但是看着你的问题,我认为你正在寻找类似的东西:
l = []
#build list
char = 'a'
if char in l:
print('error')
else:
l.append(char)
答案 1 :(得分:0)
通常使用一套来跟踪这些事情。
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)
。使用列表,效率较低,但在您的情况下,效率低下是完全不可检测的。
使用set
的目的是传达不应存在重复字母的想法。 (你知道,程序阅读的次数比写的要多得多。)