0

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'))
2
  • 1
    You're creating the list inside the loop. Commented Apr 24, 2022 at 21:32
  • as jon says, move y = [] outside of the for loop then it won't be recreated empty every time Commented Apr 24, 2022 at 21:34

4 Answers 4

2
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

Sign up to request clarification or add additional context in comments.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
I think the line where you call new_row() is indented too much, and your range loop needs a colon(:).
0

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'))

Comments

0
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

Comments

-1

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'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.