因此,我坚持检查单词中是否存在字母。游戏需要让一个人猜出单词中的字母,并判断单词中是否存在该字母(仅限5次尝试)
import random
i=0
WORDS= ("notebook","pc", "footprint")
word = random.choice(WORDS)
print(len(word))
while i<5:
inp = input("What's your guess for a letter in the word?\n")
for j in range(0,len(word)):
if inp == word[j]:
print("Yes, we have this letter.")
else:
print("No, we don't have this letter.")
i=i+1
预期输出将是一个句子,或者确认字母是否存在于单词中,或者反对。但是,实际输出是给定单词中的每个字母输出一个句子,如下所示: 您猜单词中的字母是什么? p 是的,我们有这封信。 不,我们没有这封信。
答案 0 :(得分:1)
代替检查单词的每个字母(并因此每次打印),只需检查字母是否为ResumeThread
单词即可:
import serial
import time
import serial.tools.list_ports
comlist = serial.tools.list_ports.comports()
connected = []
for ports in comlist:
connected.append(ports.device)
print("All ports on this Computer are: " +
str(connected))
console = serial.Serial(ports[0])
while 1:
console.write(b'+')
time.sleep(0.2)
console.write(b'1')
time.sleep(0.2)
def read_from_console(console):
bytes_to_be_read = console.inWaiting()
while True:
if bytes_to_be_read:
output =
console.read(bytes_to_be_read)
return output.decode()
else:
return False
read_from_console(console)
答案 1 :(得分:0)
您可以尝试使用正则表达式:
import random
import re
i=0
WORDS= ("notebook","pc", "footprint")
word = random.choice(WORDS)
print(len(word))
while i<5:
inp = input("What's your guess for a letter in the word?\n")
res = re.findall(inp, word)
length = len(res)
if length == 0:
print("Your guess is wrong :( ")
else:
print(f"You guess is right :) ")
i +=1
这里正则表达式的输出是一个列表,因此您可以使用它进行任何操作,例如hang子手游戏或...