2013-10-20 93 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