Python 如何在给定次数的尝试后终止此循环?
在大多数情况下,它似乎是工作,但不是我如何试图让它工作。当我运行它时,我被允许尝试猜测单词中的每个字母,直到我拼出单词为止 但这不是我想要的,我试图用单个字母给用户5次猜测,如果字母在单词中,那么它会告诉他们“是”,在我的单词中有一个(n)(用户猜测),但是如果字母不在我的单词中,那么它会告诉他们“否”,在我的单词中没有一个(n)(用户猜测) 经过5次尝试,我想让他们猜出不同的字母,但我不知道怎么猜 这就是我现在拥有的:Python 如何在给定次数的尝试后终止此循环?,python,Python,在大多数情况下,它似乎是工作,但不是我如何试图让它工作。当我运行它时,我被允许尝试猜测单词中的每个字母,直到我拼出单词为止 但这不是我想要的,我试图用单个字母给用户5次猜测,如果字母在单词中,那么它会告诉他们“是”,在我的单词中有一个(n)(用户猜测),但是如果字母不在我的单词中,那么它会告诉他们“否”,在我的单词中没有一个(n)(用户猜测) 经过5次尝试,我想让他们猜出不同的字母,但我不知道怎么猜 这就是我现在拥有的: import random def get_word(): wo
import random
def get_word():
words = ['cat', 'dog', 'man', 'fox', 'jar']
return random.choice(words).upper()
def check(word,guesses,guess):
status = ''
matches = 0
for letter in word:
if letter in guesses:
status += letter
else:
status += '*'
if letter == guess:
matches += 1
count = 0
limit = 5
if matches > 1:
print ('Yes! there is a(n)',guess,' in my word.')
guesses += guess
elif matches ==1:
print ('Yes! there is a(n)',guess,' in my word.')
guesses += guess
while count > limit:
input('What do you think my word is')
else:
print('No, there is not a(n)',guess,' in my word.')
guesses += guess
while count > limit:
input('What do you think my word is')
return status
def main():
word = get_word()
guesses = ""
guessed = False
print ('I am thinking of a 3 letter word with no repeating letters. You get five guesses of the letters in my word and then you have to guess the word.')
while not guessed:
text = 'Guess a letter in my word:'
guess = input(text)
guess = guess.upper()
count = 0
limit = 5
if guess in guesses:
print ('You already guessed "' + guess + '"')
elif len(guess) == len(word):
guesses += guess
if guess == word:
guessed = True
else:
print('No, there is not a(n) "' + guess + '"')
elif len(guess) == 1:
guesses += guess
result = check(word,guesses,guess)
if result == word:
guessed = True
else:
print (result)
else:
print ('Invalid entry.')
print ('Yes! you correctly guessed')
main()
对于初学者来说,
在没有猜测时:
将继续,直到猜测为真->单词被猜测。接下来,如果您希望有5次猜测,那么您需要对范围(0,5)中的i进行猜测:
然后运行猜测逻辑
if result == word:
guessed = True
与
以正确的猜测打破循环。然后,在循环之外,检查是否已经猜测,如果没有,允许猜测
另外,作为旁注,您应该检查他们是否输入了一个字符,例如
guess = input(text)
while len(guess) != 1:
guess = input(text)
我尽量保留你的原始代码和想法。我做的主要改变是去掉了check函数,因为我觉得它没有做任何有用的事情。我还将猜测变量更改为一个列表,并利用它的属性进行评估
import random
def get_word():
words = ['cat', 'dog', 'man', 'fox', 'jar']
return random.choice(words).upper()
def main():
word = get_word()
guesses = []
guessed = False
print('I am thinking of a 3 letter word with no repeating letters.'
' You get five guesses of the letters in my word and then you have'
' to guess the word.\n')
while not guessed and len(guesses)<5:
text = 'Guess a letter in my word:\n'
guess = input(text)
guess = guess.upper()
if guess in guesses:
print ('You already guessed "', guess, '"\n')
elif len(guess) > 1:
guesses.append(guess)
if guess == word:
guessed = True
else:
print('No, ', guess, 'is not the word!\n')
elif len(guess) == 1:
guesses.append(guess)
if guess in word:
print('Yes there is a(n) ', guess, 'in the word!\n')
else:
print('No, there is not a(n)', guess, 'in the word!\n')
else:
print ('Invalid entry.\n')
if guessed:
print ('You correctly guessed the word!\n')
else:
correct_guesses = [guess for guess in guesses if guess in word]
print('Last chance, what is the full word? (HINT: You have'
'correctly guessed ', str(correct_guesses), ')"')
fword = input()
if fword.upper() == word:
print('You correctly guessed the word!\n')
else: print('Tough luck! You are out of chances! The correct'
'word was ', word, '\n')
main()
playing = True
while playing:
print('Would you like to play again?')
answ = input()
if answ.upper() == 'YES':
main()
else:
print('Thank you for playing.')
playing = False
随机导入
def get_word():
words=[“猫”、“狗”、“人”、“狐狸”、“罐子”]
返回random.choice(单词).upper()
def main():
word=get_word()
猜测=[]
猜测=错误
print('我想到的是一个3个字母的单词,没有重复字母。'
“你猜了我单词中的五个字母,然后你猜到了”
'来猜单词。\n')
虽然没有猜测和len(猜测)1:
猜测。追加(猜测)
如果猜测==单词:
猜测=正确
其他:
打印('No',猜测,'不是单词!\n')
elif len(猜测)=1:
猜测。追加(猜测)
如果用词猜测:
打印('是有一个(n)',猜测,'在单词中!\n')
其他:
打印(‘不,没有(n)’,猜测‘在单词中!\n’)
其他:
打印('无效条目。\n')
如果猜测:
print('您猜对了单词!\n')
其他:
更正猜测=[如果在word中进行猜测,则在猜测中进行猜测]
打印('最后一次机会,完整的单词是什么?(提示:你有'
“正确猜测”,str(正确猜测),“)”)
fword=输入()
如果fword.upper()==单词:
print('您猜对了单词!\n')
否则:打印('真倒霉!你没有机会了!正确的'
“单词是”,单词“\n”)
main()
玩=真
玩的时候:
打印('您想再玩一次吗?')
answ=输入()
如果answ.upper()=“是”:
main()
其他:
打印('谢谢您的播放')
播放=错误
欢迎使用Stack Overflow!请将您的代码粘贴到问题中,不要链接到外部站点。您在循环中每次都设置count=0
,并且从不增加它。并且count=0
再次出现在check()中
,所以每次调用都设置为0
,但是count
在check()
范围内的版本永远不会增加。一种更好的功能分解方法是:(1)在main()
中执行所有input()
并打印;(2)执行check())
一个简单的函数,它只需要获取它所需的所有信息(包括过去的猜测历史)并返回报告。@Barmar Ohh,这是有道理的。在每次尝试猜测一个字母后,我应该如何使计数增加1,并限制尝试5次?@gojomo我应该如何增加它?正如你可能知道的,我对这一点非常陌生
import random
def get_word():
words = ['cat', 'dog', 'man', 'fox', 'jar']
return random.choice(words).upper()
def main():
word = get_word()
guesses = []
guessed = False
print('I am thinking of a 3 letter word with no repeating letters.'
' You get five guesses of the letters in my word and then you have'
' to guess the word.\n')
while not guessed and len(guesses)<5:
text = 'Guess a letter in my word:\n'
guess = input(text)
guess = guess.upper()
if guess in guesses:
print ('You already guessed "', guess, '"\n')
elif len(guess) > 1:
guesses.append(guess)
if guess == word:
guessed = True
else:
print('No, ', guess, 'is not the word!\n')
elif len(guess) == 1:
guesses.append(guess)
if guess in word:
print('Yes there is a(n) ', guess, 'in the word!\n')
else:
print('No, there is not a(n)', guess, 'in the word!\n')
else:
print ('Invalid entry.\n')
if guessed:
print ('You correctly guessed the word!\n')
else:
correct_guesses = [guess for guess in guesses if guess in word]
print('Last chance, what is the full word? (HINT: You have'
'correctly guessed ', str(correct_guesses), ')"')
fword = input()
if fword.upper() == word:
print('You correctly guessed the word!\n')
else: print('Tough luck! You are out of chances! The correct'
'word was ', word, '\n')
main()
playing = True
while playing:
print('Would you like to play again?')
answ = input()
if answ.upper() == 'YES':
main()
else:
print('Thank you for playing.')
playing = False