问题描述:

I am getting this error when I try to populate users First_Name in a csv file. Can someone show me what I am doing wrong

 import csv

# Define users

valid_input = False

while not valid_input:

users =raw_input('Number of users: ')

try:

users = range(0,int(users))

valid_input = True

except:

print "Invalid input"

pass

First_Name = ["Test"+str(user) for user in range (1, users+1)]

Last_Name = ["User%s" %user for user in users]

Email_Addresses = []

for user in users:

email= raw_input("Email domain for user %d: " %user)

Email_Addresses.append(Last_Name[user] + email)

Password = ["Password1" for user in users]

Group =["Test" for user in users]

Admin = ["Yes" for user in users]

# open a file for writing.

# open a file for writing.

with open('users.csv', 'wb') as csv_out:

writer = csv.writer(csv_out)

writer.writerows(zip(Email_Addresses, Password, First_Name, Last_Name, Group, Admin))

This is the Traceback I am getting

Traceback (most recent call last):

First_Name = ["Test"+str(user) for user in range (1, users+1)]

TypeError: can only concatenate list (not "int") to list

网友答案:

users is a list:

users = range(0,int(users))

but you then try to create a new range from that list:

First_Name = ["Test"+str(user) for user in  range (1, users+1)]

where users + 1 is an attempt to add 1 to a list.

Don't do that. Just loop over your existing range:

First_Name = ["Test"+str(user + 1) for user in users]
相关阅读:
Top