그래서 기본적으로 여기에 있습니다.파이썬 - 사용자가 동일한 글자를 두 번 입력하지 않도록하는 게임을 생각하십시오
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 함수를 사용하여 데이터를 저장해야한다는 것을 알고 있습니다. 그러나 나는 그것을 시도하고 여러 가지 방법으로 그것은 단지 작동하지 않는 것 같습니다. 나는 어디에서 잘못하고 있는지 아무도 모른다. 누구든지이 질문에 답할 수 있다면 좋을 것이다. 나는 그것이 어떻게 생겼는지 그리고 어디에서 코드에 넣어야 하는지를 알아야합니다. 감사!
와우 이것은 실제로 내 코드에서 잘 작동합니다. 고마워요! –