所以我有一个文件,看起来像这样
2
3
1 2 3 4 5 6
所以我需要用三个独立的变量来读取这个文件。对于前两个变量,我用了类似这样的方法。
guests = int(input_file.readline())
length = int(input_file.readline())
但是第三行我需要一个列表,我怎么把它转化为一个整数,我试过这个。
sticks = input_file.readline()
sticks = [int(i) for i in sticks]
但它给我一个错误。invalid literal for int() with base 10
解决方案:
你需要使用 split()
在一行中读取多个整数
sticks = [int(i) for i in sticks.split()]
本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/700.html