if 循环中的语句不起作用
Posted
技术标签:
【中文标题】if 循环中的语句不起作用【英文标题】:If statements in while loop not working 【发布时间】:2016-01-08 16:51:52 【问题描述】: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 = ""
count = 1
score = 100
hint = ""
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("Your guess: ").lower()
while(guess != correct) and (guess != ""):
print("Sorry, that's not it.")
guess = input("Your guess: ").lower()
count += 1
if count == 3:
option = input("Would you like a hint? (yes/no)" + '\n').lower()
if option == "yes":
hint = "yes"
if word == "python":
print("HINT: This was used to make this game.")
elif word == "jumble":
print("HINT: The name of the game.")
elif word == "easy":
print("HINT: Part of the Staples slogan.")
elif word == "difficult":
print("HINT: A synonym for hard.")
elif word == "answer":
print("HINT: Opposite of question.")
elif word == "xylophone":
print("HINT: An instrument.")
if guess == correct:
print("That's it! You guess it!\n")
score = score - 5*count
if hint == "yes":
score = score - 20
print("Thanks for playing. The word was", correct, ".")
print("You score is: " + str(score))
input("\n\nPress the enter key to exit.")
所以我在 python 中制作了这个小游戏,基本上要求用户解读单词,我在执行所有 if 语句时遇到问题。当我运行我的程序时,它可以正常工作到它向用户询问提示的部分。在回答是提示时,它会循环回到 while 循环,而不是显示提示,它只是说“对不起,不是这样”并继续循环直到用户得到正确的答案。因此,代码中的以下 if 语句没有被执行,有人可以解释为什么会这样以及如何解决它吗?
if option == "yes":
hint = "yes"
if word == "python":
print("HINT: This was used to make this game.")
elif word == "jumble":
print("HINT: The name of the game.")
elif word == "easy":
print("HINT: Part of the Staples slogan.")
elif word == "difficult":
print("HINT: A synonym for hard.")
elif word == "answer":
print("HINT: Opposite of question.")
elif word == "xylophone":
print("HINT: An instrument.")
【问题讨论】:
【参考方案1】:问题出在这里:
option = input("Would you like a hint? (yes/no)" + '\n').lower
option
被设置为lower
,一个字符串方法。你需要调用它:
option = input("Would you like a hint? (yes/no)" + '\n').lower()
^^
现在option
将是一个字符串。
【讨论】:
我刚刚注意到,当我发布问题时,它对问题没有影响,问题仍然存在 如果程序之前缺少()
,那将使其无法运行。添加()
应该会有所作为。您是否保存了源代码然后重新运行它?【参考方案2】:
您正在检查word
是什么,但我认为这不是答案(它已被更改)。改为检查correct
:
if option == "yes":
hint = "yes"
if correct == "python":
print("HINT: This was used to make this game.")
elif correct == "jumble":
print("HINT: The name of the game.")
elif correct == "easy":
print("HINT: Part of the Staples slogan.")
elif correct == "difficult":
print("HINT: A synonym for hard.")
elif correct == "answer":
print("HINT: Opposite of question.")
elif correct == "xylophone":
print("HINT: An instrument.")
【讨论】:
以前试过这个,没有运气 等等,它确实起作用了,在修复了 Tigerhawk 提到的错误并将其更改为更正之后,它起作用了,我的问题是,为什么它是正确的还是单词有关系?两者都包含我想要的实际单词 @W.Ahmed - 在while word:
循环中,word
最终会变成一个空字符串,jumble
将包含混乱的单词,而correct
包含原始单词。以上是关于if 循环中的语句不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Liquid - if contains 语句在 for 循环中不起作用
如果全局不起作用,在Python中访问for循环之外的变量?
Javascript:document.getElementById.innerHTML 中的 if-else 语句不起作用