python的while循环时if不能打印_while循环中的If语句不起作用

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语句时遇到了问题。当我运行我的程序时,它可以很好地工作到它向用户请求提示的部分。在为提示回答yes时,它会循环回while循环,而不是显示提示,而是说“对不起,不是这样的”,并继续循环,直到用户得到正确的答案。所以下面代码中的if语句没有被执行,有人能解释一下为什么会这样,以及我如何修复它吗?在

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值