我正在开发一个简单的程序,程序将文本文件作为列表读取,然后以网格样式显示列表。我想在屏幕上显示列表,我已设法做到这一点,但我不断在每个单词的结尾处获得/ r / n。你如何删除/ r / n?此外,如果我想在下一行a,b和d之前清除行a,b和c,我将如何清除屏幕(它是OS系统CLS)还是只打印50个空白行的循环?
这是我的程序到目前为止(不完整),我知道可能有更好的方法,但对我来说这是一个开始,有没有更好的方法,人们会建议改善(好,我改善后)咨询)。
import time
print ('welcome to the missing word guessing game: ')
print ('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
menu = ('Menu \n 1. Easy game - press e \n 2. Hard game - press h \n 3. Quit - press q')
print (menu)
user_input = raw_input('Please type your response: ')
if user_input == 'e':
file =open('words.txt', 'r')
word_list = file.readlines()
row_a = word_list [0:3]
row_b = word_list [3:6]
row_c = word_list [6:9]
row_d = word_list [7:10]
print (row_a)
print (row_b)
print (row_c)
print ('study the words')
time.sleep (5)
print ('Please wait - Do not touch any thing - just study')
time.sleep (5)
print ('Now study the words again, can you spot the word that is missing? ')
print (row_a)
print (row_b)
print (row_d)
user_answer = raw_input('Can you guess the word that is missing? ')
if user_answer == 'brown':
print ('Well done')
print (menu)
else:
print ('Wrong answer')
答案 0 :(得分:0)
要删除所有换行符,只需执行以下操作:
word_list = file.read().splitlines() # Instead of readlines()
是的,要清除屏幕,我只会产生一个子流程:
from subprocess import call
call('cls', shell=True)