while循环中的打印功能不打印

print function in while loop not printing

我是 python 的新手,我遇到了几件可疑的事情。我想弄清楚我在这个问题上做错了什么:

本练习假定您已完成编程练习 7,随机数文件 作家。编写另一个程序从文件中读取随机数,显示 numbers,然后显示以下数据: • 总人数 • 从文件中读取的随机数个数

正在写入文件:

import random

def main():

    randomcount = int(input('How many numbers should i generate?'))

    randomfile = open('randomnumber.txt', 'w')

    total = 0
    for numbers in range(randomcount):
        number = random.randint(1,100)
        total+= number
        randomfile.write((str(number)) + '\n')
        randomfile.write((str(total)) + '\n') 

    randomfile.close()
    print('File updated')

main()

输出:

How many numbers should i generate?5 (enter)
file updated                    -       **Question 1**
file updated                     | ---- this is new.. while doing 
file updated                    -       trial and error this started
                                        repeating. First 2 times then 
                                        after awhile 3 times. 
                                        Refreshed kernel and outputs 
                                        still does this

读取文件:<-- #主要问题

def main():
    randomfile = open('randomnumber.txt','r')

    contents = randomfile.readline()

    while randomfile !='':
        total = randomfile.readline()

        contents = contents.rstrip('\n')
        total = total.rstrip('\n')

        print(contents)

        contents = randomfile.readline()

   print('total: ',total)
   randomfile.close()

main()

输出:

90                      -
22                       |
17                       |--- Randomly generated
2                        |
75                      -
        **Question 2**
        <--- print('total: ', total) not showing up


        -
         |
         |
         |
         .   **Question 3**
         .   <--------- Space goes on forever like if its printing 
         .              space. so much that theres a scroll bar just
         .              for empty space. if i try to scroll all the 
         |              way to the bottom so that i can see if maybe 
         |              its printing at the end i never reach the end 
         |              of it because for some reason the program 
        -               keeps adding more and more space.

希望将 readline 更改为 readlines,这应该有效

很明显,问题是在您的第一行中,您以单引号 ' 开头并以双引号 " 结尾。变化:

def main():

    randomcount = int(input('How many numbers should i generate?"))

至:

def main():

    randomcount = int(input('How many numbers should i generate?'))

问题是写入总数的行在每次迭代中都被执行。这就是为什么行数是生成数字的两倍。

解决方案是取消该行的缩进,我在这里就是这样做的:

import random

def main():

    randomcount = int(input('How many numbers should i generate?'))

    randomfile = open('randomnumber.txt', 'w')

    total = 0
    for numbers in range(randomcount):
        number = random.randint(1,100)
        total+= number
        randomfile.write((str(number)) + '\n')
    randomfile.write((str(total)) + '\n') # <<<<<<<<<<<<<<This line was indented, it is fixed now.

    randomfile.close()
    print('File updated')

main()

编辑:修复读取功能:

def main():
    randomfile = open('randomnumber.txt', 'r')
    lines = randomfile.readlines()
    total = 0

    for line in lines:
        if line is lines[-1]:
            print(f"Total: {line.replace('\n', '')}") #Since the write function includes the total as the last line, you don't need to calculate here.
        else:
            print(line.replace('\n', ''))

main()

我重组了我的代码并将其设为 for 循环而不是 while 循环。您不需要重新剥离正在转换为 int 的数字。将累加器放在输入文件部分而不是输出文件部分。 Mae 代码更干净,它有效!

随机导入

def main():

    randomcount = int(input('How many numbers should i generate?'))

    randomfile = open('randomnumber.txt', 'w')

    for numbers in range(1,randomcount + 1):
        numbers = random.randint(1,100)

        randomfile.write(str(numbers) + '\n') 

    randomfile.close()

    randomfile = open('randomnumber.txt','r')
    total = 0

    for numbers in randomfile:
        numbers = int(numbers)
        total+= numbers

        print(numbers)
    print('total',total)

main()