2013-10-20 186 views
0

我是Python的初學者,我被困在練習中。在書中有一個叫做Word Jumple的遊戲。以下是我需要做的: 改善「Word Jumble」,使每個單詞與提示配對。 如果玩家卡住了,他應該能夠看到提示。 添加一個評分系統,可以獎勵解決雜亂問題的玩家而不要求提示。Python中的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) 

# create a variable to use later to see if the guess is correct 
correct = word 

# create a jumbled version of the word 
jumble ="" 
hint = "False" 

while word: 
    position = random.randrange(len(word)) 
    jumble += word[position] 
    word = word[:position] + word[(position + 1):] 

# 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 != "": 
    if guess == "hint" and word == "python": 
     hint = "True" 
     print("It's a snake") 
     guess = input("Your guess: ") 
    elif guess == "hint" and word == "jumble": 
     hint = "True" 
     print("It's a game") 
     guess = input("Your guess: ") 
    elif guess == "hint" and word == "easy": 
     hint = "True" 
     print("It's type of difficulty") 
     guess = input("Your guess: ") 
    elif guess == "hint" and word == "difficulty": 
     hint = "True" 
     print("It's type of difficulty") 
     guess = input("Your guess: ") 
    elif guess == "hint" and word == "answer": 
     hint = "True" 
     print("It's the opposite of question") 
     guess = input("Your guess: ") 
    elif guess == "hint" and word == "xylophone": 
     hint = "True" 
     print("Don't know WTF is that") 
     guess = input("Your guess: ") 
    else: 
     print("Sorry, that's not it.") 
     guess = input("Your guess: ") 

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

if hint == "False": 
    print("Great! You did it without a hint") 
else: 
    print("Dat hint, man") 

print("Thanks for playing.") 
input("\n\nPress the enter key to exit.") 

因此,我有這樣的:

Welcome to Word Jumble! 
Unscramble the letters to make a word. 
(Press the enter key at the prompt to quit.) 

The jumble is: jbelum 

Your guess: hint 
Sorry, that's not it. 
Your guess: jumble 
That's it! You guessed it! 

Great! You did it without a hint 
Thanks for playing. 


Press the enter key to exit. 

爲什麼while循環丟失了所有當輸入爲「提示」,並直接進入else子句?

在此先感謝您的時間和幫助。

回答

2

的問題是,你到了while guess != correct and guess != "":循環的時間,word已經完全混亂。因此,在該循環中的所有ifelif語句中,您永遠不會有word等於列表中的六個單詞之一。

要解決這個問題,在這些語句代替correctword

if guess == "hint" and correct == "python": 
    hint = "True" 
    print("It's a snake") 
    guess = input("Your guess: ") 
elif guess == "hint" and correct == "jumble": 
    hint = "True" 
    print("It's a game") 
    guess = input("Your guess: ") 
elif guess == "hint" and correct == "easy": 
    hint = "True" 
    print("It's type of difficulty") 
    guess = input("Your guess: ") 
elif guess == "hint" and correct == "difficulty": 
    hint = "True" 
    print("It's type of difficulty") 
    guess = input("Your guess: ") 
elif guess == "hint" and correct == "answer": 
    hint = "True" 
    print("It's the opposite of question") 
    guess = input("Your guess: ") 
elif guess == "hint" and correct == "xylophone": 
    hint = "True" 
    print("Don't know WTF is that") 
    guess = input("Your guess: ") 
else: 
    print("Sorry, that's not it.") 
    guess = input("Your guess: ") 
+0

哦,上帝,我沒有意識到,當隨機字母從它實際上修改它取字...謝謝:) – Terrax

+0

小心,小心,小心......以下任何一個語句都不會改變'word':'word [position]','word [:position]'或'word [(position + 1):]'的值。這三個返回從'word'的內容中提取的新字符串,但它們本身不影響'word'。不,改變'word'的是'word = word [:position] + word [(position + 1):''''中的'word ='。這取得右側的值,並將其分配到左側(在這種情況下恰好是「單詞」)。那有意義嗎? –

+0

完美:)謝謝 – Terrax

0

這是使你的程序更簡單的一種方法:

jumble = ''.join(random.sample(word, len(word))) 
1

musical_coder是正確的,解決你的問題。更多的事情情侶,你應該在你的代碼修改,使其更好:

不要使用提示變量字符串,使用布爾:

代替:

hint = "False" 
hint = "True" 

使用:

hint = False 
hint = True 

你也可以改寫while循環一點點

guess = input("Your guess: ") 

只有一次,而不是每次如果。

此外,改變結果打印有點這樣,即使你輸入空字符串和退出遊戲它會打印提示信息。

所以它可能是這個樣子:

print("The jumble is:", jumble) 
guess = "dummy" 
while guess != correct and guess != "": 
    guess = raw_input("Your guess: ") 
    if guess == "hint" and correct == "python": 
     hint = True 
     print("It's a snake") 
    elif guess == "hint" and correct == "jumble": 
     hint = True 
     print("It's a game") 
    elif guess == "hint" and correct == "easy": 
     hint = True 
     print("It's type of difficulty") 
    elif guess == "hint" and correct == "difficulty": 
     hint = True 
     print("It's type of difficulty") 
    elif guess == "hint" and correct == "answer": 
     hint = True 
     print("It's the opposite of question") 
    elif guess == "hint" and correct == "xylophone": 
     hint = True 
     print("Don't know WTF is that") 
    else: 
     print("Sorry, that's not it.") 

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

    if not hint: 
     print("Great! You did it without a hint") 
    else: 
     print("Dat hint, man") 

print("Thanks for playing.") 
input("\n\nPress the enter key to exit.") 
+0

謝謝你這個iblazevic。我首先用布爾值做了這個,但我不確定,我認爲這是造成這個問題的原因。但是,當我嘗試這樣做的Python迴應有關的raw_input錯誤: 回溯(最近通話最後一個): 文件「輸出省略」 37行,在 猜測=的raw_input(「你的猜測:」) NameError:名字'raw_input'未定義 這是爲什麼?當我改變它輸入它的作品...這是Python 3.3.2。在2.7.5中,當我使用輸入時出現錯誤,並且在使用raw_input時,它都是很好的。 – Terrax

+0

沒問題,關於輸入和raw_input你可以閱讀這個問題http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3x – iblazevic