python question printing a list

ghost dog1

Seniorius Lurkius
5
Hi,

I'm trying to learn python programming. I have a question.

This is my script:

Declare my variables
strName = '' # The name of the item
strValue = '' # The estimated value of the item
srChoice = '' # A choice by the user
lstRow = []
lstTable=[]

# Step 1: Display a menu of choices to the user
while (True): # Step 1
print("""
Menu of Options
1) Add Data to List
2) Display Current Data
3) Exit and Save to File
""")
strChoice = str(input("which option would you like to perform? [1 to 3]: "))
if strChoice == '1':

print("Type in a 'Name' and Value fo Household Items")
strName = str(input(" Enter a Name: "))
strValue = str(input(" Enter a Value: "))
lstRow = [strName, strValue]
lstTable += lstRow
elif strChoice == '2':
print('Your Current Data is')
for row in lstTable:
print('', row[0], ',', row[1])
elif strChoice == '3':
print('Option 3')
break #Exits out Loop
else:
print('Please only choose 1, 2, or 3 !')

When I ran the program:
which option would you like to perform? [1 to 3]: 1
Type in a 'Name' and Value fo Household Items
Enter a Name: lmap
Enter a Value: 30
Menu of Options
1) Add Data to List
2) Display Current Data
3) Exit and Save to File

which option would you like to perform? [1 to 3]: 2
Your Current Data is
l , m
3 , 0

I believe it should say lmap, 30. Why is the print statement only giving me that type of output?

Thank you
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Hi,

I'm trying to learn python programming. I have a question.

This is my script:

Declare my variables
Code:
strName = '' # The name of the item
strValue = '' # The estimated value of the item
srChoice = '' # A choice by the user
lstRow = []
lstTable=[]

# Step 1: Display a menu of choices to the user
while (True): # Step 1
    print("""
    Menu of Options
    1) Add Data to List
    2) Display Current Data
    3) Exit and Save to File
    """)
    strChoice = str(input("which option would you like to perform? [1 to 3]: "))
    if strChoice == '1':

        print("Type in a 'Name' and Value fo Household Items")
        strName = str(input(" Enter a Name: "))
        strValue = str(input(" Enter a Value: "))
        lstRow = [strName, strValue]
        lstTable += lstRow
    elif strChoice == '2':
        print('Your Current Data is')
        for row in lstTable:
            print('', row[0], ',', row[1])
    elif strChoice == '3':
        print('Option 3')
        break #Exits out Loop
    else:
        print('Please only choose 1, 2, or 3 !')
When I ran the program:
Code:
which option would you like to perform? [1 to 3]: 1
Type in a 'Name' and Value fo Household Items
 Enter a Name: lmap
 Enter a Value: 30
    Menu of Options
    1) Add Data to List
    2) Display Current Data
    3) Exit and Save to File
    
which option would you like to perform? [1 to 3]: 2
Your Current Data is
 l , m
 3 , 0

I believe it should say lmap, 30. Why is the print statement only giving me that type of output?

Thank you

Remember, white space matters, especially in python. To get the forum to maintain your whitespace, you need to use
Code:
 tags.

Now for some feedback: first, get rid of all the [c]str[/c] and [c]lst[/c] stuff.  Remember, your code exists for another human being to read.  That stuff is ultimately distracting.

There is no need to predeclare variables, it is also not considered best practice.

Next, input returns a string, there is no need to [c]str[/c] it.

Finally [c][] + ['lmap', '30'][/c] does not equal what you think it does.
 

ghost dog1

Seniorius Lurkius
5
I thought by typing it would append a new row lstRow to lstTable list. But it looks like using the debugger it that it just adds it to the 1st row rather than adding another row.

lstTable +=lstRow

I had to use this command to append to a new row: lstTable.append(lstRow)

but when I did this it worked:
lstTable +=[lstRow]

So I'm not sure what is the difference between:
lstTable +=lstRow
and lstTable += [lstRow]

Does anybody have any insight? I would appreciate it. I'm trying to learn Python.
 

spiralscratch

Ars Praefectus
3,103
Subscriptor
lstTable += lstRow is equivalent to lstTable.extend(lstRow). It basically just combines the two list objects into one and places the result into lstTable.

lstTable += [lstRow] is essentially equivalent to lstTable.append(lstRow) (and a weird way to do it). The list object stored in lstRow is added to another list as the sole item, which is then combined with lstTable.

You'll probably make your life easier if you use the built-in list functions:
https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range

Also, if you're looking to learn Python, RealPython has a number of free and paid courses that are pretty thorough. They usually come up early in a search like "python <something> howto", along with a few other similar good sites. While they currently have nothing, Humble Bundle usually puts up a collection of Python books every two to three months.

You may also want to look over the Python Style Guide before you develop any bad habits.

A Python IDE (I like PyCharm) can also be very useful.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
I thought by typing it would append a new row lstRow to lstTable list. But it looks like using the debugger it that it just adds it to the 1st row rather than adding another row.

lstTable +=lstRow

I had to use this command to append to a new row: lstTable.append(lstRow)

but when I did this it worked:
lstTable +=[lstRow]

So I'm not sure what is the difference between:
lstTable +=lstRow
and lstTable += [lstRow]

Does anybody have any insight? I would appreciate it. I'm trying to learn Python.

In Python, for two lists, + is concatenate. So, when you say [1,2] + [2,3] it concatenates the two lists giving [1,2,3,4]. Whereas when you say lstTable + [lstRow], and lstRow is a list, then it is more like [1,2] + [[3,4]] The same concatenate results in [1,2,[3,4]]