2013-10-21 12 views
0
print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!") 

words= ['utopian','fairy','tree','monday','blue'] 

i=int(input("Please enter a number (0<=number<10) to choose the word in the list: ")) 

if(words[i]): 
    print("The length of the word is: " , len(words[i])) 


guess=input("Please enter the letter you guess: ") 


if(guess in words[i]): 
    print("The letter is in the word.") 

else: 
    print("The letter is not in the word.") 

guesses=1 

while guesses<6: 
    guess=input("Please enter the letter you guess: ") 


if(guess in words[i]): 
    print("The letter is in the word.") 
    guesses=guesses+1 

else: 
    print("The letter is not in the word.") 
    guesses=guesses+1 

if guesses==6: 

    print("Failure. The word was:" , words[i]) 

이 행맨 프로그램을 Python으로 시작했습니다. 저는 일련의 지시에 기반한 단계별로이 작업을 수행하고 있습니다. 입력 한 문자가 선택한 단어에서 발견되는지 여부를 확인하는 간단한 코드를 작성하려고합니다. 나는 나쁜 성향의 숫자를 세는 것과 관련된 성냥의 입장을 무시하고있다. 지금까지는 그렇게 좋았지 만 작지만 큰 문제가 발생했습니다. 여섯 번째로 이라고 생각합니다. 루프가 끝나기를 원하고 사용자가 실패하고 컴퓨터가 이겼다는 것을 알립니다. 내 경우에는 사용자가 여섯 번째 추측을 입력하면 루프가 끝났음을 알게되었습니다. 따라서 단어가 '요정'또는 무엇이든간에 사용자가 루프를 추측 한 올바른 문자가 아무리 많아도 여섯 번째 이동과 관계없이 마칩니다. 나는 사용자가 여섯 가지 나쁜 추측을 입력했을 때만 루프를 끝내기를 원합니다. 따라서 'fairy'라는 단어의 예에서 사용자가 'f'를 입력하면 올바른 것이고 다음 여섯 번의 추측이 잘못된 경우 "실패"메시지가 표시됩니다 내가 지금 가지고있는 것과 반대로 인쇄 될 것입니다. 당신이 그 마지막 줄을 제거하면행맨 프로그램 (Python 3.x)

if(guess in words[i]): 
    print("The letter is in the word.") 
    guesses=guesses+1 

, 다음 좋은 추측이 당신에 대해 계산되지 않습니다 :

+1

올바른 추측과 잘못된 추측을 따로 계산하는 방법은 어떻습니까? –

+1

Jon Clements가 옳습니다. 당신은 나쁜 추측뿐만 아니라 추측시기를 세고 있습니다. 문자가 단어에있을 때 추측 변수를 증가시키지 않아야합니다. – Schickmeister

답변

1

는이 코드가 있습니다.

또한 공백에주의해야합니다. 지금 질문하시는 방식대로 guess=input("Please enter the letter you guess: ") 행만 while 루프에 있습니다. StackOverflow에 코드를 두는 간단한 실수라고 생각합니다.

+0

고마워요. 꽃 봉오리 :) –

1

다소 독창적 인 대답이지만, 여기에 몇 가지 편집 된 코드가 있습니다.

print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!") 

words= ['utopian','fairy','tree','monday','blue'] 

i=int(raw_input("Please enter any number to choose the word in the list: "))%5 
#the %6 here means divide the input by six and take the remainder as the answer. I found the instructions a little confusing, and this allows the user to be competely unrestricted in the number that they choose while still giving you a number (0, 1, 2, 3, or 4) that you want. Increasing the number after the % will give you a larger variety of choices. 

# if(words[i]): 
#this line is unnecessary with the above code, and also would not prevent errors futher along in the code if i was not a number between 0 and 4. 

print "Your chosen word is", len(words[i]), "characters long." 
# slight wording change here 
#guess=input("Please enter the letter you guess: ") 
#if(guess in words[i]): 
#print("The letter is in the word.") 
#else: 
#print("The letter is not in the word.") 
#guesses=1 
# all of this is already accomplished in the loop you wrote below. 

incorrect_guesses = 0 
# it's always nice to initialize a variable 
while incorrect_guesses<6: 
    guess=str(raw_input("Please enter a letter to guess: ")) 
    # Unless you're using python 3, I would use raw_input rather than input. 


    if((guess) in words[i]): 
     print('Correct! The letter "' + guess + '" appears in the word ' + str(words[i].count(str(guess))) + ' times.') 
    else: 
     print('Sorry, the letter "' + guess + 
       '" is not in the word.') 
     incorrect_guesses=incorrect_guesses+1 
# Plusses instead of commas clean up the output a bit, and str(words[i].count(str(guess))) gives you the string form of the number which indicates how many times the guess appears in the word. This is useful in words like "tree". 
print("Failure. The word was:" , words[i]) 
# This is outside the while loop because it gets triggered as soon as incorrect guesses go over 6. 
# You can still improve the program by adding a feature which tells the players that they have guessed all of the correct letter and telling them what the word was, and possibly by increasing the word list. You can also tell players when they have already guessed a letter in case they've forgotten. 

여러분의 미래의 파이썬 노력에 도움이되기를 바랍니다.