Python Forum
User input/picking from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User input/picking from a list
#1
I am trying to figure out how to use user input to select items from a list and display how much the item is. This is what I have so far.

# Make a program with user input that shows what items on the grocery list cost what:

apples = 1.99
bananas = 2.99
oranges = 3.99

item_pull = input("What item would you like to see the price of?: ")
Reply
#2
there is no list whatsoever in your code. use a data structure like dict.

read https://nedbatchelder.com/blog/201112/ke...names.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Like buran indicated, python dictionaries make things like this incredibly easy.

items = {
	'apples': 1.99,
	'bananas': 299,
	'oranges': 399 }

item_pull = input ('What item would you like to see the price of? : ')

if item_pull in items :
	print (f'The price of {item_pull} is ${items [item_pull]}.') 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exit function from nested function based on user input Turtle 5 330 Oct-10-2021, 12:55 AM
Last Post: Turtle
  Unable to access the user input value given to daterangepicker klllmmm 1 704 May-26-2021, 09:16 PM
Last Post: nilamo
  Problem restricting user input in my rock paper scissors game ashergreen 6 1,240 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Looking for a way to loop until user enters from a list? PythonW19 7 1,006 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  LinkedList by User Input SantiagoPB 5 1,277 Mar-03-2021, 09:54 PM
Last Post: deanhystad
  Picking a function to execute palladium 1 474 Feb-09-2021, 04:47 PM
Last Post: deanhystad
  Generate Random operator, take user input and validate the user mapypy 4 1,067 Feb-03-2021, 08:41 PM
Last Post: nilamo
Question Reset list if user regrets Gilush 1 893 Dec-05-2020, 10:55 AM
Last Post: Gilush
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 1,205 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 1,045 Sep-22-2020, 04:46 PM
Last Post: Parshaw

Forum Jump:

User Panel Messages

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