with open("data.txt",'r') as f:
player1 = f.readline()
player2 = f.readline()
print (player1+" you are blue")
print (player2+" you are red")
我的文本文件是
sam
jim
在单独一行
当前输出为
sam
you are blue
jim
you are red
我想让它输出
sam you are blue
jim you are red
我已经到处看了看,但似乎找不到答案我也尝试了一个。split和做一个单词计数,但我不能让它工作
您需要使用strip来删除尾随的新行
with open ("data.txt",'r') as file:
player1 = file.readline()
player2 = file.readline()
print(player1.strip() + " you are blue" )
print(player2.strip() + " you are red")