Reputation: 29
I'm trying to create lists/ records using the input function and 'for' loops, but I can't seem to get more than one list to populate. Ideally, I like to be able to define the number of sub-lists I want to create and then input the information per list until the total number of specified lists has been created.
def new_row(n):
for i in n:
y = []
month = input('Enter the month: ')
city = input('Enter the city: ')
numCoups = input('Enter the number of coupons accepted: ')
numAds = input('Enter the number of advertisments ran: ')
l = [month, city, numCoups, numAds]
y.append(l)
new_row(input('How many entries do you wish to input'))
Upvotes: 0
Views: 115
Reputation: 56
if n is a number you should change n to range(n) it's unclear n, also you have to let y grow by lefting it outside the loop because it will always be dscarded, finally you must return the value of y because if you don't do this you are also discarding it, maybe you can pass it as a parameter if you want to reuse it. For Example:
def new_row(n, y=[]):
for i in range(n):
month = input('Enter the month: ')
city = input('Enter the city: ')
numCoups = input('Enter the number of coupons accepted: ')
numAds = input('Enter the number of advertisments ran: ')
l = [month, city, numCoups, numAds]
y.append(l)
return y
result_rows=new_row(input('How many entries do you wish to input'))
Upvotes: -1
Reputation:
def new_row(n):
y=[] #outside the loop
for i in range(n)#range function:
month = input('Enter the month: ')
city = input('Enter the city: ')
numCoups = input('Enter the number of coupons accepted: ')
numAds = input('Enter the number of advertisments ran: ')
l = [month, city, numCoups, numAds]
y.append(l)
new_row(int(input('How many entries do you wish to input'))) #int function
You should declare the list outside the loop and you should convert the input to an integer, because by default it's a string,you should use range func in for loop
Upvotes: 2
Reputation: 427
def new_row(n):
y = [] # outside the loop, otherwise it keeps on getting reset
for i in range(n): # or [for i in range(int(n)):], if n is a string
month = input('Enter the month: ')
city = input('Enter the city: ')
numCoups = input('Enter the number of coupons accepted: ')
numAds = input('Enter the number of advertisments ran: ')
l = [month, city, numCoups, numAds]
y.append(l)
new_row(int(input('How many entries do you wish to input')))
#you need to do int(input()) because input normally returns a string, but you want a number
Upvotes: 0
Reputation: 3472
You need to create the initial list outside the for loop and you need to convert the input to an integer and use range(n) instead of n:
def new_row(n):
y = []
for i in range(int(n)):
month = input('Enter the month: ')
city = input('Enter the city: ')
numCoups = input('Enter the number of coupons accepted: ')
numAds = input('Enter the number of advertisments ran: ')
l = [month, city, numCoups, numAds]
y.append(l)
return y
new_row(input('How many entries do you wish to input'))
Upvotes: 0