如何输入列表中单词的一部分字符并将其打印出来



我试图把hangman作为我自己的小项目,但我不知道如何输入一个字符,这个字符是列表中某个单词的一部分,并将其打印出来。我怎么能那样做?

import random
answer = input("Hi, wanna play? Y/N: ")

if answer == "N" or answer == "n":
print("ok")
elif answer == "Y" or answer == "y":
list = ["hey", "hello", "hi"]
list = random.choice(list)
len(list) == print(len(list) * "-")
print(len(list))
answer = input("ok, guess a letter: ")
if answer == list:
print("good")
else:
print("bad")

这在Python中非常简单:

if character in word:
#do stuff
print(character)

这个游戏需要以下内容(我已经编辑了您的代码(:

import random
import re
answer = input("Hi, wanna play? Y/N: ")
if answer == "N" or answer == "n":
print("ok")
elif answer == "Y" or answer == "y":
myList = ["heyyyy", "helloo", "hiii"]
myWord = random.choice(myList)
emptyDash = "-" *len(myWord) 
print(emptyDash)
while emptyDash != myWord:
answer = input("ok, guess a letter: ")
if answer in myWord and answer != "":
print("good")
emptyDash = list(emptyDash)
indicies = [m.start() for m in re.finditer(answer, myWord)]
for value in indicies:
emptyDash[value] = answer
emptyDash = "".join(emptyDash)
print(emptyDash)
else:
print("bad")

输出示例:

Hi, wanna play? Y/N: Y
------
ok, guess a letter: h
good
h-----
ok, guess a letter: i
bad
ok, guess a letter: y
good
h-yyyy
ok, guess a letter: e
good
heyyyy

请注意,不应将任何值分配给预定义的变量,如listdict等。此外,我还使用正则表达式模块来查找单词中出现的字符。

此外,不应使用双等号(==(分配变量。这个符号只是为了表达一个合乎逻辑的陈述。

检查单词中是否有字符:

if char in word:
print(char)

相关内容

  • 没有找到相关文章

最新更新