Home > OS >  Hello, i want to make a program where an empty list is populated by an input from the user
Hello, i want to make a program where an empty list is populated by an input from the user

Time:05-11

def passanger_list(passangerInput, pp): 
    pp = ["passangers:"]
    passangerInput = input("what is your passanger name?")   
    if passangerInput:
        pp.append()

print(passanger_list)

CodePudding user response:

Maybe this will help you:

def passanger_list(passangerInput, pp): 
    if passangerInput:
        pp.append(passangerInput)
    return pp

pp = []
pp = passanger_list(
    input("what is your passanger name?"),
    pp
)

print("passangers:", pp)

Also, you might want to spell it "passengers" instead of "passangers".

CodePudding user response:

def passanger_list(): 
    pp = ["passangers:"]
    passangerInput = input("what is your passanger name?")   
    if passangerInput:
        pp.append(passangerInput)
    return pp

print(passanger_list())

CodePudding user response:

If you define pp = ["passangers:"] why do you want to get input for your function?

global pp 
pp= {"passengers":[]}

def passanger_list(): 
    passangerInput = input("what is your passanger name?")
    
    pp["passengers"].append(passangerInput) 
   

    return pp

>>> passanger_list()

CodePudding user response:

Do it this way

''' You dont need to send pp and passengerInput as parameters for this function because, they're values are being initialized only when the passenger_list() is called. ''' 
def passenger_list(): 
    ''' It would be better for you to avoid having the passengers text inside. If you really want the values as passenger: "name of passenger", you can use a dictionary. I will add code for that as well.'''
    pp = []
    passengerInput = input("what is your passanger name?")   
    if passengerInput:
        pp.append(passengerInput)
    print(pp)
    
    # This statement is crucial so that the value is passed back to the code that is calling it.
    return pp

In this case, after calling passenger_list(), your output will look like ["Ram", "Shyam", "Sita"].

For output of the form - {"Passengers":["Ram","Shyam","Sita]}, please refer the code below.


def passenger_list(): 
    
    pp = {"Passengers":[]}
    passengerInput = input("what is your passanger name?")   
    if passengerInput:
        pp["Passengers"].append(passengerInput)
    print(pp)
    # You can access the list using pp["Passenggers"]
    print(pp["Passengers"]) # Output is ["Ram", "Shyam", "Sita"]  
    
    return pp 

Read more about Python dictionaries here.

CodePudding user response:

I think it is better to use a list of dictionaries instead of a list.

Instead of this:

    pp = ["passangers:"]
    passangerInput = input("what is your passanger name?")   
    if passangerInput:
        pp.append()

print(passanger_list)

I would do:

passenger_list = []

def add_passenger(passenger_name): 
    if passanger_name:
        passenger_list.append({"name":passenger_name})

add_passenger("bob")
print(passanger_list)

This way you can store multiple passengers and can even add other key-value pairs like seat number or class etc.

The way you are doing it will make it harder to retrieve information.

  • Related