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
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 292 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 454 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 232 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 194 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 291 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 278 Mar-21-2024, 09:31 PM
Last Post: max22
  difference between forms of input a list to function akbarza 6 1,038 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Reading and storing a line of output from pexpect child eagerissac 1 4,270 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  unbounded variable akbarza 3 505 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 665 Jan-20-2024, 09:20 PM
Last Post: Learner1

Forum Jump:

User Panel Messages

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