2013-09-29 2 views
1

컴퓨터가 임의의 단어를 고르고 그 단어를 추측해야하는 게임을 만들어야합니다. 컴퓨터는 단어에 몇 개의 글자가 있는지 플레이어에게 알려줍니다. 그런 다음 플레이어는 편지에 단어가 있는지 5 가지 기회를 묻습니다. 컴퓨터는 "yes" 또는 "no"으로 만 응답 할 수 있습니다. 그런 다음 플레이어는 그 단어를 추측해야합니다. 내가 가지고있는 것 :문자열에서 정보를 가져 와서 출력하는 방법은 무엇입니까?

import random 
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle") 
word = random.choice(WORDS) 
print(len(word)) 
correct = word 
guess = input("\nYour guess: ") 
if guess != correct and guess != "" : 
     print("No.") 

if guess == correct: 
    print("Yes!\n") 

이 문제를 해결하는 방법을 모릅니다.

+1

어떻게 당신이 손으로 그것을 할 것입니다 아래처럼 뭔가를 찾고있다? 정말 높은 수준이라 할지라도 의사 코드를 작성하십시오. – Blender

+0

카운터 및 while 루프 사용 –

답변

0

당신은

import random 

WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle") 

word = random.choice(WORDS) 
correct_answer = word 
max_guesses = 5 

print("Word length:", len(word)) 
print("Attempts Available:", max_guesses) 


for guesses in range(max_guesses): 
    guess = input("\nEnter your guess, or a letter: ") 
    if guess == correct_answer: 
     print("Yay! '%s' is the correct answer.\n" % guess) 
     break 
    elif guess != "": 
     if guess[0] in correct_answer: 
      print("Yes, '%s' appears in the answer" % guess[0]) 
     else: 
      print("No, '%s' does not appear in the answer" % guess[0]) 
else: 
    print("\nYou ran out of maximumum tries!\n") 
+0

고맙습니다 – user2829036

1

글자가 단어에 5 번이면 사용자가 컴퓨터에 질문하게한다고 가정합니다. 그렇다면, 여기에 코드입니다 :

for i in range(5): #Let the player ask 5 times 
    letter = input("What letter do you want to ask about? ")[0] 
    #take only the 1st letter if they try to cheat 

    if letter in correct: 
     print("yes, letter is in word\n") 
    else: 
     print("no, letter is not in word") 

키가 for 루프 내에서 in 연산자입니다.

관련 문제