【问题标题】:strings can't be print from text file无法从文本文件中打印字符串
【发布时间】:2012-11-05 21:02:40
【问题描述】:

所以我正在做一个测验 python 程序,我的程序似乎有一些问题。 第一次如果您想开始测验,测验将开始并像普通测验一样打印出问题,当您回答正确的问题时,我会给您积分。然而 测验结束后,您想再次进行测验,这就是问题开始的地方,当我要开始测验时,不会打印出任何问题

while True:
    print('1. Take test, 2. Add Question, 3. Modify, 4. Delete, 5. Exit')
    n=input('Choice: ')
    counter=0
    lines=q.readlines()
    liness=p.read()
    key=liness.split('\n')
    while n not in ('1','2','3','4','5'):
        print('Invalid Choice')
        n=input('Choice: ')

    if n=='1':            
        score=0
        counter=0
        n=0
        nb=0

        while True:
            linez=lines[n:n+5]
            for line in linez:
                print(line)
            b=CheckAnswer()
            if b==key[nb]:
                score=score+1
                print('Nice')
            n=n+6
            nb=nb+1
            counter=counter+1
            print('Current Score: ',score)
            if counter>=len(key):
                break

谁能帮我解决这个问题?

【问题讨论】:

    标签: python file list loops while-loop


    【解决方案1】:

    在你执行了这些行之后:

    lines=q.readlines()
    liness=p.read()
    

    文件指针 p 和 q 指向文件末尾。以下对readlinesread 的调用将从文件末尾开始,因此将返回一个空答案...

    您可以使用q.tell() 来检查您在文件中的实际位置,q.seek(0) 会将指针重置为文件的开头...

    在这种特殊情况下,从文件中读取后添加以下行将解决问题:

    q.seek(0)
    p.seek(0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      相关资源
      最近更新 更多