2013-10-21 4 views
0

나는 while 루프의 포맷팅을 진행할 것이고, 나는 이것을 고칠 수있는 방법에 대해 확신 할 수 없다. (나는 초보자 다. 어떤 도움이라도 대단히 감사하겠습니다! 나는 사용자가 힌트를 요청할 때 '미안하지만 그 메시지가 아닙니다'라는 메시지가 나타나기를 원하지 않는다.while 루프가 시작될 때 나는 그것을하지 않기를 원한다.

# Word Jumble 
# 
# The computer picks a random word and then "jumbles" it 
# The player has to guess the original word 

import random 

# create a sequence of words to choose from 
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") 
# pick one word randomly from the sequence 
word = random.choice(WORDS) 

hint = '' 

if word == 'python': 
    hint = 'snake' 
if word == 'jumble': 
    hint = 'jumble' 
if word == 'easy': 
    hint = 'opposite of hard' 
if word == 'difficult': 
    hint = 'opposite of easy' 
if word == 'answer': 
    hint = 'question' 
if word == 'xylophone': 
    hint = 'dingding' 
# create a variable to use later to see if the guess is correct 
correct = word 

# create a jumbled version of the word 
jumble ="" 
while word: 
    position = random.randrange(len(word)) 
    jumble += word[position] 
    word = word[:position] + word[(position + 1):] 


count = 0 

# start the game 
print(
""" 
      Welcome to Word Jumble! 

    Unscramble the letters to make a word. 
(Press the enter key at the prompt to quit.) 
""" 
) 
print("The jumble is:", jumble) 

guess = input("\nYour guess: ") 
while guess != correct and guess != "": 
    print("Sorry, that's not it.") 
    count += 1 
    hint_input = input('would you like a hint') 
    if hint_input == 'y': 
     print(hint) 
    else: 
     guess = input("Your guess: ") 

if guess == correct: 
    print("That's it! You guessed it!\n") 

print("Thanks for playing.") 

input("\n\nPress the enter key to exit.") 
+1

어떤 오류 메시지가 나타납니다? – CDspace

+0

전체 스택 추적을 제공하여 코드에 무엇이 잘못되었는지 확인할 수 있습니다. – Paco

+0

죄송합니다. 내 말씨가 꺼져있었습니다. 오류가 아닙니다. 설명에서 수정 됨 – Zack

답변

2

다음을 제거하십시오. 사용자가 힌트를 받았는지 여부에 관계없이 항상 새로운 추측을 원하기 때문입니다.

if hint_input == 'y': 
     print(hint) 
    guess = input("Your guess: ") 
0

코드는 잘 작동 -하지만 당신은 입력 필드에 " 또는 '을 포함한 문자열을 입력 할 것으로 예상된다. 따라서 "jumble"을 입력하고 jumble 또는 "y"이 아닌 y이 아닌을 입력하십시오.

(글쎄, 거기에 몇 가지 이상한 논리가있다, 예를 들어, 힌트를주고 싶은지 다시 한 번 묻는다. -이 동작을 제거하기 위해 else을 제거한다.) 적어도 적어도 작동한다.)

0

코드를 복사하여 실행했습니다.
입력을 raw_input으로 변경했습니다. 입력을 훨씬 더 멋지게 만듭니다. 삭제 된 기타 : 인쇄 후 (힌트)로 문제를 해결해야합니다. 마지막으로, "힌트를 주시겠습니까?"뒤에 추가 공간을 추가했습니다. - 읽기가 더 쉬워졌습니다.

# Word Jumble 
# 
# The computer picks a random word and then "jumbles" it 
# The player has to guess the original word 

import random 

# create a sequence of words to choose from 
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") 
# pick one word randomly from the sequence 
word = random.choice(WORDS) 

hint = '' 

if word == 'python': 
    hint = 'snake' 
if word == 'jumble': 
    hint = 'jumble' 
if word == 'easy': 
    hint = 'opposite of hard' 
if word == 'difficult': 
    hint = 'opposite of easy' 
if word == 'answer': 
    hint = 'question' 
if word == 'xylophone': 
    hint = 'dingding' 
# create a variable to use later to see if the guess is correct 
correct = word 

# create a jumbled version of the word 
jumble ="" 
while word: 
    position = random.randrange(len(word)) 
    jumble += word[position] 
    word = word[:position] + word[(position + 1):] 


count = 0 

# start the game 
print(
""" 
      Welcome to Word Jumble! 

    Unscramble the letters to make a word. 
(Press the enter key at the prompt to quit.) 
""" 
) 
print("The jumble is:", jumble) 

guess = raw_input("\nYour guess: ") 
while guess != correct and guess != "": 
    print("Sorry, that's not it.") 
    count += 1 
    hint_input = raw_input('would you like a hint? ') 
    if hint_input == 'y': 
     print(hint) 

    guess = raw_input("Your guess: ") 

if guess == correct: 
    print("That's it! You guessed it!\n") 

print("Thanks for playing.") 

raw_input("\n\nPress the enter key to exit.") 
+0

비평 해 주셔서 감사합니다. – Zack

+0

답변을 게시하는 동안 몇 명의 사람들이 답변하고 댓글을 달았습니다. raw_input이 Python 3에서 Python 2.7 (내가 사용하는)에서와 다르게 작동하면 해당 부분을 무시하십시오. – Roman

+0

@Zack : 천만에. 희망은 귀하의 컴퓨터에서 답변을 잘 작동합니다. – Roman

관련 문제