我正在创建一个名为OpenCity的项目,其中包含一个高级版本。高级套餐是问题所在。
项目层次结构为:
opencity // is a project in PyCharm 2019.3
premium // is a package
__init__.py // is a module
premium.py // is a module
premium_user.py // is a module
premium_user.txt // is a text file
premium_user.py:
import random as ran
def premium_users_adder():
premium_user1a = open('premium_user.txt', 'a')
# premium_user1w = open('premium_user.txt', 'w')
# premium_user1r = open('premium_user.txt', 'r')
p2 = int(input("How many members do you want to add to premium_users to database? "))
p1 = []
p3 = []
for i in range(p2):
member = input("Type the person's name. ")
p1.append(member)
id1 = ran.randint(100000000, 99999999999)
p3.append(str(id1))
for i, e in zip(p1, p3):
premium_user1a.write(i + ' ' + e + '\n')
premium_user1a.close()
print()
print("Done")
def premium_users_checker():
premium_user1r = open('premium_user.txt', 'r')
p2 = []
p4 = []
p5 = []
for data in premium_user1r:
p2.append(data)
for i in range(len(p2)):
x = int(i)
p4[x], p5[x] = p2[x].split(' ')
print(p4, p5)
premium.py:
import premium_users as pu
pu.premium_users_checker()
运行premium.py时出错:
Traceback (most recent call last):
File "F:/PyCharm Python Works/OpenCity/premium/premium.py", line 3, in <module>
pu.premium_users_checker()
File "F:\PyCharm Python Works\OpenCity\premium\premium_users.py", line 32, in premium_users_checker
p4[x], p5[x] = p2[x].split(' ')
IndexError: list assignment index out of range
除了包含高级代码的premium_users.txt
以外,我已经提供了所有内容。
答案 0 :(得分:0)
我找到了解决问题的方法, 答案是
def premium_users_checker():
premium_user1r = open('premium_user.txt', 'r')
p2 = []
p4 = []
p5 = []
for data in premium_user1r:
p2.append(data)
for i in range(len(p2)):
x1 = int(i)
p10, p11 = p2[x1].split(' ')
p4.append(p10), p5.append(p11)
print(p4, p5)
如果有人遇到问题,请使用此简单修复程序, 创建两个变量, 将它们放在for循环中,在该循环中,循环将列表分成两部分。 将数字一一保存在变量上,并将其附加在列表中。 你得到了他们的全部。