gpt4 book ai didi

python - 这是 python 中 `in` 关键字的正确使用吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:45 25 4
gpt4 key购买 nike

我试图通过使用 if/in 检查来制作一个非常基本的刽子手游戏,但是,代码没有响应玩家输入的更改。由于这是我第一次使用 in 关键字,我想问一下这是否正确。

我尝试了其他一些方法,例如字符串查找和单独将字母制成字符串(这很困惑),if/in似乎是最有效的方法完成任务。如果还有其他方法,请告诉我。

#put word into here, as a string :)

print("Welcome to Hangman")
word = ["a", "v", "a", "c" "a", "d", "o"]

wordLength = len(word) + 1
lives = wordLength * 2

print("lives:", lives)
print("Letters in word", wordLength)

guess = input("h")

while lives != 0:
if guess in word:
print("YES!")
print(guess)
index = word.index(guess)
print(index)
else:
print("Wrong!")
lives - 1
pass
pass


while lives == 0:
print("falied! try again")
pass

理想的结果是当输入与上述字符串中的字母匹配时,控制台将打印“YES!”和这封信,或者如果不打印“错误”并夺走一条生命。

最佳答案

请检查此代码是否适合您。我稍微修改了一下,添加了一些条件。

#put word into here, as a string :)

print("Welcome to Hangman")
word = ["a", "v", "a", "c", "a", "d", "o"]

wordLength = len(word) + 1
lives = wordLength * 2

print("lives:", lives)
print("Letters in word", wordLength)

guess = input("Enter the letter : ")

while lives != 0:
if guess in word:
print("YES!")
print(guess)
index = word.index(guess)
#print("Index = ",index)
del word[index]
else:
print("Wrong!")
lives -=1
print("lives:", lives)

if len(word)==0 and lives > 0:
print("Success!!")
break
guess = input("Enter the letter : ")



if lives == 0:
print("falied! try again")

关于python - 这是 python 中 `in` 关键字的正确使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55493562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com