Python Forum
Taking user input and storing that to a variable then storing that variable to a list
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Taking user input and storing that to a variable then storing that variable to a list
#1
Hey Guys,

I'm new to Python programming and am having trouble finding some information on some coding I am trying to write for fun. I have some experience with c++ programming so i understand some of the basic structure of coding, however i can't find a way to get the program I am working on to do what i need it to.

Basically i am trying to take user input and store it to a variable and store that variable to a list defined by another variable then be able to retrieve that user input by having the user enter a number so...

user inputs item_number as 75 lets say 75 is for this purpose a serial number
user inputs item_name as hat
user inputs item_price as 10.25

this would be the information for item1

so item1 = [item_number, item_name, item_price]??

then if the user chooses to list all items stored i would print item1
where the items and there info would be listed 
1: s/n 75
    hat
    10.25

formatting aside at this time I want the user to be able to reference the s/n but not actually have to enter the s/n to select the number

firstly can a variable that is defined with user input from an if statement be stored in a list as i have it here above

and if it can then is it as simple as assigning that "item1" to be printed if the user inputs say the number 1??
Reply
#2
def user_input():
    item_number = input("Item number: ")
    item_name = input("Item name: ")
    item_price = input("Item price: ")
    return item_number, item_name, item_price
Now you can call the functions when you want to add new items.

items = []
items.append(user_input())
And print the items

for number, name, price in items:
    print(number, name, price) # no formating
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Ok I will work on this and see if i can integrate this into the code!
At least i know where to start now

Thanks wavic
Reply
#4
Yes, you can do this. You could have three input statements to assign values to three variables, item_number, item_name, item_price. You could then make a list of those triplets of information, call it items. Then you can add your triplet to that list with append:
items.append[(item_number, item_name, item_price)]
Note that I used parentheses (()) instead of brackets ([]). That appends a tuple of the three variables to the list items. Assuming some parsing of the user input, the list might look like this:
items = [(75, 'hat', 10.75), (801, 'mat', 10.80), (23, 'cat', 5.00)]
Note that Python is 0 indexed. that means items[1] is equal to (801, 'mat', 10,80), the second item. To get the first item, you would use item[0]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Traceback (most recent call last):
File "auction.py", line 35, in <module>
items.append[(item_number, item_name, item_price)]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'


this is the error i get

i have entered the items.append to the end if an if statement so when the user chooses to add an item they will be asked for the info for that item
i have also defined a variable items = [()]
is the above variable needed in this way
my thought was to reserve space for the user input
Reply
#6
Show us your code.

You need a list of items so define items as [].
When you get the input you just add it as a tuple.

>>> def user_input():
...     item_number = input("Item number: ")
...     item_name = input("Item name: ")
...     item_price = input("Item price: ")
...     return item_number, item_name, item_price

>>> items = []
... for _ in range(3):
...     items.append(user_input())
Item number: 30
Item name: car
Item price: 23000
Item number: 32
Item name: fridge
Item price: 800
Item number: 44
Item name: cat
Item price: 300

>>> items
[('30', 'car', '23000'), ('32', 'fridge', '800'), ('44', 'cat', '300')]
The function returns a tuple. So if you do it that way or you get the items without a function and append them like @ichabod801, you end with this - list of tuples representing the items
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
items.append is a function, you need to use parenthesis (, not square brackets [, so
items.append( [item_number, items_name, item_price] )
Reply
#8
items = [()]

while(1):

#       item_number_1 = [iten_number, iten_name, iten_price]
       print "what would you like to do?"
       print "1: Add item"
       print "2: edit item"
       print "3: remove item"
       print "4: list current items"
       option = raw_input( "please select an option ")
       if (option=="1"):
               print "we will now enter the information for this item"
               item_number = raw_input("what is the item number ")
               item_name = raw_input("what is the name of this item? ")
               item_price = raw_input("how much does this item cost? ")
               print "your items number is "
               print item_number
               print "your items name is "
               print item_name
               print "your items price is "
               print item_price
               items.append[(item_number, item_name, item_price)]

       if option=="4":
               print items
disregard options 2 and 3 ill deal with those later  with the code you just provided ill have to go back and change things for sure

Moderator Larz60+: Please use code tags. see: https://python-forum.io/misc.php?action=help&hid=25 I added them for you this time
Reply
#9
See @zivoni's post.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
zivoni i did have the signs reversed and once i fixed that it worked so now i am able to print the list items by selecting option 4
wavic how would i use your code in what im trying here?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 256 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 503 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  How to revert back to a previous line from user input Sharkenn64u 2 823 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Storing DictProxy object and modifying its nested value? haihal 3 965 Dec-20-2024, 04:58 AM
Last Post: deanhystad
  LLM data I/O - for storing notes Alkanet 0 325 Dec-19-2024, 09:08 AM
Last Post: Alkanet
  creating arbitrary local variable names Skaperen 9 1,760 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 2,611 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  User input with while loops chizzy101010 2 4,847 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  Storing data in readable place user_404_lost_and_found 4 1,388 Jul-22-2024, 06:14 AM
Last Post: Pedroski55
  how solve: local variable referenced before assignment ? trix 5 1,674 Jun-15-2024, 07:15 PM
Last Post: trix

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020