我的文本文件:
apple, computer pie yellow
strawberry , happy
pink , desk yellow cream
ice, maths happy
我的代码:
import random
with open('fruit.txt','r') as f:
myline =random.choice(f.readlines())
x = myline.split(",")
print(x[0].strip() +", "+ ''.join(i[0]
for i in x[1].strip().split(" ")))
它随机打印,从文本文件开始一行,输出逗号前的单词和逗号后每个单词的第一个字母。 例如,输出为:
apple, cpy
然后,用户需要猜测逗号后的单词。.我如何匹配用户输入的文本文件中的正确行? 帮助我不知道..请最简单的方法!
答案 0 :(得分:1)
您可以使用内置功能input
import random
with open('fruit.txt','r') as f:
myline =random.choice(f.readlines())
x = myline.split(",")
print(x[0].strip())
answers = x[1].strip().split(" ")))
user_answer = input("guess a word: ")
if user_answer in answers:
print("Correct!")
else:
print("Wrong!)
答案 1 :(得分:0)
您可以使用内置功能输入。
import random with open('fruit.txt','r') as f:
myline =random.choice(f.readlines())
x = myline.split(",")
print(x[0].strip())
answers = x[1].strip().split(" ")
user_answer = input("guess a word: ")
if user_answer in answers:
print("Correct!")
else:
print("Wrong!")