我是靠谱客的博主 无辜汉堡,这篇文章主要介绍python重复上一次输入,检查输入中的重复(Python),现在分享给大家,希望可以做个参考。

How do i check for a repeating input(letter) for a hangman game?

Example:

word is apple

input is Guess a letter: a

output is Well Done!

then guess next word

input is Guess a letter: a

output should be you already guess that letter.

my codes:

def checkValidGuess():

word = getHiddenWord()

lives = 10

num = ["1","2","3","4","5","6","7","8","9",]

#guessed = ''

while lives != 0 and word:

print("nYou have", lives,"guesses left")

letter = input("Guess a letter or enter '0' to guess the word: ")

if letter.lower() in word:

print("Well done!", letter, "is in my word!")

lives -= 1

elif len(letter)>1:

print("You can only guess one letter at a time!")

print("Try again!")

elif letter in num:

print("You can only input letter a - z!")

print("Try again!")

#elif letter in guessed:

#print("repeat")

elif letter == "0":

wword = input("What is the word?").lower()

if wword == word:

print("Well done! You got the word correct!")

break

else:

print("Uh oh! That is not the word!")

lives -= 1

#elif letter == "":

#print("Uh oh! the letter you entered is not in my word.")

#print("Try again!")

else:

print("Uh oh! the letter you entered is not in my word.")

print("Try again!")

lives -= 1

Thanks.

解决方案

Here's an easy way. Start by initializing a list:

guesses = []

Then in your loop:

letter = input("Guess a letter or enter '0' to guess the word: ")

if letter in guesses:

print("Already guessed!")

continue

guesses.append(letter)

最后

以上就是无辜汉堡最近收集整理的关于python重复上一次输入,检查输入中的重复(Python)的全部内容,更多相关python重复上一次输入,检查输入中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(154)

评论列表共有 0 条评论

立即
投稿
返回
顶部