![]() |
Taking user input and storing that to a variable then storing that variable to a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Taking user input and storing that to a variable then storing that variable to a list (/thread-2602.html) Pages:
1
2
|
Taking user input and storing that to a variable then storing that variable to a list - jowalk - Mar-27-2017 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?? RE: Taking user input and storing that to a variable then storing that variable to a list - wavic - Mar-27-2017 def user_input(): item_number = input("Item number: ") item_name = input("Item name: ") item_price = input("Item price: ") return item_number, item_name, item_priceNow 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 RE: Taking user input and storing that to a variable then storing that variable to a list - jowalk - Mar-27-2017 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 RE: Taking user input and storing that to a variable then storing that variable to a list - ichabod801 - Mar-27-2017 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]
RE: Taking user input and storing that to a variable then storing that variable to a list - jowalk - Mar-27-2017 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 RE: Taking user input and storing that to a variable then storing that variable to a list - wavic - Mar-27-2017 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 RE: Taking user input and storing that to a variable then storing that variable to a list - zivoni - Mar-27-2017 items.append is a function, you need to use parenthesis ( , not square brackets [ , soitems.append( [item_number, items_name, item_price] ) RE: Taking user input and storing that to a variable then storing that variable to a list - jowalk - Mar-27-2017 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 itemsdisregard 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
RE: Taking user input and storing that to a variable then storing that variable to a list - wavic - Mar-27-2017 See @zivoni's post. RE: Taking user input and storing that to a variable then storing that variable to a list - jowalk - Mar-27-2017 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? |