Python while循环测试两个true/false元素
我正在为我的孩子们做一个单词拼图游戏。我希望他们能够在猜测中键入“hint”以获得提示。第一个“提示”应该给出单词的第一个和最后一个字母。下次他们键入“hint”时,应提供单词的前两个和后两个字母。。等等 我让它第一次工作,他们键入'提示',但随后while循环被打破,他们不能猜错或再次键入'提示' 我知道问题在于这一行:Python while循环测试两个true/false元素,python,while-loop,Python,While Loop,我正在为我的孩子们做一个单词拼图游戏。我希望他们能够在猜测中键入“hint”以获得提示。第一个“提示”应该给出单词的第一个和最后一个字母。下次他们键入“hint”时,应提供单词的前两个和后两个字母。。等等 我让它第一次工作,他们键入'提示',但随后while循环被打破,他们不能猜错或再次键入'提示' 我知道问题在于这一行: while guess != correct and guess != 'hint': 我似乎无法修复它,以便用户可以多次键入提示 这是我的密码: # The comput
while guess != correct and guess != 'hint':
我似乎无法修复它,以便用户可以多次键入提示
这是我的密码:
# The computer picks a random word, and then "jumbles" it
# the player has to guess the original word
import random
replay = "y"
while replay == "y":
print("\n"* 100)
word = input("Choose a word for your opponent to de-code: ")
print("\n"* 100)
# provide a hint if the user wants one
hint_count = 1
# create a variable to use later to see if the guess is correct
correct = word
# create a empty jumble word
jumble = ""
# while the chosen word has letters in it
while word:
position = random.randrange(len(word))
# add the random letter to the jumble word
jumble += word[position]
# extract a random letter from the chosen word
word = word[:position] + word[position +1:]
# start the game
print(
"""
Welcome to the Word Jumble!
Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)
"""
)
score = 10
print("The jumble is: ",jumble)
guess = input("\nType 'hint' for help but lose 3 points. \
\nYOUR GUESS: ")
while guess != correct and guess != 'hint':
print("Sorry that's not it.")
score -= 1
guess = input("Your guess: ")
if guess == 'hint':
print("The word starts with '"+ correct[0:hint_count]+"' and ends with '"+correct[len(correct)-hint_count:len(correct)]+"'")
score -= 3
hint_count += 1
guess = input("Your guess: ")
if guess == correct:
print("That's it! You guessed it!\n")
print("Your score is ",score)
print("Thanks for playing.")
replay = input("\n\nWould you like to play again (y/n).")
问题是您在while循环之外检查
guess==“hint”
。试试像这样的东西
while True:
guess = input(...)
if guess == correct:
# correct
break
elif guess == 'hint':
# show hint
else:
# not it, decrement score
问题是您在while循环之外检查
guess==“hint”
。试试像这样的东西
while True:
guess = input(...)
if guess == correct:
# correct
break
elif guess == 'hint':
# show hint
else:
# not it, decrement score
在第一个“提示”之后,程序应该询问猜测,然后继续打印(“感谢播放”),因为检查提示的条件在while循环之外。将其放入while循环:
while guess != correct:
if guess == 'hint':
print("The word starts with '"+ correct[0:hint_count]+"' and ends with '"+correct[len(correct)-hint_count:len(correct)]+"'")
score -= 3
hint_count += 1
guess = input("Your guess: ")
else:
print("Sorry that's not it.")
score -= 1
guess = input("Your guess: ")
在第一个“提示”之后,程序应该询问猜测,然后继续打印(“感谢播放”),因为检查提示的条件在while循环之外。将其放入while循环:
while guess != correct:
if guess == 'hint':
print("The word starts with '"+ correct[0:hint_count]+"' and ends with '"+correct[len(correct)-hint_count:len(correct)]+"'")
score -= 3
hint_count += 1
guess = input("Your guess: ")
else:
print("Sorry that's not it.")
score -= 1
guess = input("Your guess: ")
顺便说一句,有更简单的方法来创建混乱。顺便说一句,有更简单的方法来创建混乱。查看。您的问题是
while
循环允许用户输入猜测或hint
,但输入hint
会中断循环。这就是为什么用户只能使用hint
一次(在一次提示之后,用户只能再输入一个单词)。现在有几个答案,其中任何一个都可以解决这个问题。您需要一个while
循环,直到用户正确获得单词后才会退出,并且您希望对用户键入的每个答案分别进行评估(是否正确?是否为提示
?是否为退出
?),这是一个逻辑问题,不是编程问题。@KarlKnechtel编程本身就是一个逻辑问题。您的问题是,while
循环允许用户输入猜测或hint
,但输入hint
会中断循环。这就是为什么用户只能使用hint
一次(在一次提示之后,用户只能再输入一个单词)。现在有几个答案,其中任何一个都可以解决这个问题。您需要一个while
循环,直到用户正确获得单词后才会退出,并且您希望对用户键入的每个答案分别进行评估(是否正确?是否为提示
?是否为退出
?),这是一个逻辑问题,不是编程问题。@KarlKnechtel编程本身就是一个逻辑问题。