【问题标题】:remove /r /n from list and clear screen after timer从列表中删除 /r /n 并在计时器后清除屏幕
【发布时间】:2014-04-13 11:10:50
【问题描述】:

我正在开发一个简单的程序,该程序将文本文件作为列表读取,然后以网格样式显示该列表。然后我想在屏幕上显示列表,我已经成功地做到了,但我在每个单词的末尾不断得到 /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')

【问题讨论】:

    标签: python list time


    【解决方案1】:

    要删除所有换行符,只需:

    word_list = file.read().splitlines()  # Instead of readlines()
    

    是的,要清除屏幕,我只需生成一个子进程:

    from subprocess import call 
    
    call('cls', shell=True)
    

    【讨论】:

    • @P_Lee 你确定吗?文件肯定有read() 属性,字符串肯定有splitlines() 属性。确保您使用的是我发布的确切代码。
    • @P_Lee 如果你详细说明你在哪里得到这个错误,我可能会帮助你。
    • 对不起,如果我投了反对票 - 不知道我可能是因为在电话上。
    • @P_Lee 如果你投了反对票,我已经编辑了帖子,所以你可以撤消它——然后我们可以讨论AttributeError
    猜你喜欢
    • 2011-12-07
    • 2014-09-16
    • 2012-03-10
    • 2011-10-17
    • 2015-01-19
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多