0

Code:

for a in range(1,n+1):
    name = input("Enter name of passenger ")
    age = int(input("Enter age of passenger"))
    sex = input("Enter sex of passenger")
    lis= [name,age,sex]
    passengers = passengers.append(lis)

print("All passengers are :")
print(passengers)

I have tried this to make a ticket making software, but the names of passengers are not getting added to passengers list. The result shown is None.

AMC
  • 2,642
  • 7
  • 13
  • 35
  • just change `passengers = passengers.append(lis)` with `passengers.append(lis)` – kederrac Feb 08 '20 at 13:51
  • Welcome to StackOverflow. You already appear to have your answer but for future reference "My code is not running properly" is not a helpful title for a question. – David Buck Feb 08 '20 at 13:52
  • 1
    I'll also add that `for a in range(1, n+1)` is superfluous, because a isn't used in the code. `for _ in range(n)` is cleaner. – Aaron Zolnai-Lucas Feb 08 '20 at 14:01
  • This is a duplicate of https://stackoverflow.com/questions/16641119/why-does-append-always-return-none-in-python?noredirect=1&lq=1. – AMC Feb 08 '20 at 18:39

2 Answers2

2

You are assigning the result of append() to the passengers variable but append() returns None. Simply remove the assignment:

for a in range(1,n+1):
    name = input("Enter name of passenger ")
    age = int(input("Enter age of passenger"))
    sex = input("Enter sex of passenger")
    lis= [name,age,sex]
    passengers.append(lis)
0

The return value of .append() is None. Which is what you're assigning to your variable.

What you'd want instead is to define your passengers prior to your loop then append to it as follows

passengers = []
for a in range(1,n+1):
    name = input("Enter name of passenger ")
    age = int(input("Enter age of passenger"))
    sex = input("Enter sex of passenger")
    lis=[name,age,sex]
    passengers.append(lis)

print("All passengers are :")
print(passengers)
Kasem Alsharaa
  • 892
  • 1
  • 6
  • 15