0

I am new to programming and I have been running into various problems for a while now. This is a simple code where I want to be able to select an item from the store "Walmart" and based on that, access the number from walmart_prices and print that number. So, if someone selects "3", it should display the corresponding number in walmart_prices.

Here is an extremely simple code:

walmart_prices = ["10", "15", "20", "20", "25", "10", "12", "18", "12", "22"]

store = input("Walmart, Costco, or Sprouts?").lower()

if store == ("walmart"):
    w = input("1 - $10, 2 - $15, 3 - $20, 4 - $20, 5 - $25, 6 - $10, 7 - $12, 8 - $18, 9 - $12, or 10 - $22")
    print("Here are your items:", w)
    print("Your total is: $", walmart_prices[store])
tzaman
  • 46,925
  • 11
  • 90
  • 115
  • `input` gives you a string, you need to convert it to an integer before using it as an array accessor. See the linked dupe. `walmart_prices[int(w)]` should do what you want. – tzaman Feb 12 '21 at 17:55

1 Answers1

0

Sorry I can't understand your questions. When someone enters value of w , you are calling walmart_prices[walmart]. But there is nothing like this .

walmart_prices is a list. if you want to call it you need to enter integer. You can do it by

w = int(input("1 - $10, 2 - $15, 3 - $20, 4 - $20, 5 - $25, 6 - $10, 7 - $12, 8 - $18, 9 - $12, or 10 - $22"))

and change the line to

print("Your total is: $", walmart_prices[w])

I am also newbie too

  • Thank you so much, this helped a lot. It fixed the problem but another problem arose. When I try to enter multiple numbers to give me multiple values, it gives me the error: invalid literal for int() with base 10: I was wondering what the solution to this was? – Aaditya Monga Feb 12 '21 at 21:25
  • When you enter multiple value , it will be string. Firstly you need split it. may be separated by comma or space. After splitting you will get list. Then you have to iter it with for loop – Meher Nigar Feb 13 '21 at 05:39