New to python, How to add user input together

Viewed 233

I'm really new to coding and after learning all the syntax for HTML and CSS I figured I'd move on to python but I'd actually learn how to use it to become a python programmer instead of just learning the syntax

So as one of my first small projects I figured id make a small piece of code that'd ask the user for some items they're buying then add the price of those items together to give them the price of their shopping list essentially, However, I couldn't get it working (p.s I'm really new so my code will probably look dumb)

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item
choice = input("Select your item(type 'nothing' to make it stop) \n : ")
#the list the purchased items would go into
price = []
while items != 'nothing':
    input('Another item?: ')
    if choice in items:
        the_choice = price[choice]
    else:
        print("Uh oh, I don't know about that item")
6 Answers

I recommend searching about the python basics and loops

This will make it

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14,
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item

price = 0
while True:
    choice = input("Select your item(type 'nothing' to make it stop) \n : ")

    if choice == 'nothing':
        break

    if choice in items.keys():
        price += items[choice]
    else:
        print("Uh oh, I don't know about that item")

print('your result', price )

The problem with your code -

if choice in items:
        the_choice = price[choice]

In the above lines of code, what you are trying to do doesn't make sense with what you are trying to achieve. You are probably looking to add each item that user inputs into the price list. But you are instead assigning the value at choice index to the_choice variable which is wrong.


What you are trying to do can be done in the following manner -

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item
choice = input("Select your item(type 'nothing' to make it stop) \n : ")

#the list the purchased items would go into.
# If user enters input not in items, it would add 0 into price
price = [items.get(choice,0)]

# The loop will keep running until user enters nothing and it stops looping
while True:
    choice = input('Another item?: ')
    if choice in items:
        # Append price of each choice of items that user inputs
        price.append(items[choice])
    # Break whenever user enters nothing.
    elif choice == 'nothing':
        break
    # On any other input, just print that you don't know about that item
    else:
        print("Uh oh, I don't know about that item")
# At last, printing the final cost of all the items that user selected
print('Your total cost of items is - ',sum(price))

OUTPUT :

Select your item(type 'nothing' to make it stop) 
 : milk
 Another item?: bread
 Another item?: butter
 Another item?: nothing
 Your total cost of items is -  47

Also, do note that you don't need to maintain the price list unless you are going to print the price list to the user. You could just keep a variable that would add up all the sum of price of items and print it at the last. Its up to you based on what you are trying to achieve with your code.

Is this what you want?

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }



price = 0

while True:
    choice = input("Select your item(type 'nothing' to make it stop) \n : ")
    if choice in items:
        price += items[choice]
    elif choice == 'nothing':
        break
    
    else:
        print("Uh oh, I don't know about that item")
print('Your total price is: ',price)

Code:

# items and their prices
items = {
  'banana': 3,
  'apple': 2,
  'milk': 12,
  'bread': 15,
  'orange': 3,
  'cheese': 14, 
  'chicken': 42,
  'peanuts':32 ,
  'butter': 20,
  'cooldrink': 18,
}

users_items = [] # The list of purchased items
choice = None

while choice != 'nothing':
  choice = input("Select your item(type 'nothing' to make it stop) : ") # Asking for the item
  if choice in items.keys():
    users_items.append((choice, items[choice]))
  else:
    print('Item not in item\'s list')

final_cost = sum(val for item, val in users_items)
final_items = [item for item, val in users_items]
print(f'Final sum of users items is: {final_cost}. Items {final_items}')

Output:

Select your item(type 'nothing' to make it stop) 
 : banana
Select your item(type 'nothing' to make it stop)
 : nothing
Item not in item's list
Final sum of users items is: 3. Items ['banana']

Explanation

users_items is a list. When user types his choice, then program appends to list a tuple(item, price). After nothing the loop ends and results are printed. final_cost is the the sum of 2nd elements of every tuple in the users_items. final_items is the list of 1st elements of every tuple in the users_items.

@Vasyl Yovdiy has the most straightforward answer that's easy for a beginner to digest. However, it is missing the different string requirement for subsequent inputs. I would add a variable first that controls the string of your input.

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14,
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item

price = 0
first = True
while True:
    
    if first:
        choice = input("Select your item(type 'nothing' to make it stop) \n : ")
        first = False
    else:
        choice = input('Another item?: ')

    if choice == 'nothing':
        break

    if choice in items.keys():
        price += items[choice]
    else:
        print("Uh oh, I don't know about that item")

print('your result', price )

Is this what you were looking for?

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item
choice = input("Select your item(type 'nothing' to make it stop) \n : ")
a = input('Would you like to get another item? (yes/no) > ')
b = print("See you again!")
c = input('Another item? >')
#the list the purchased items would go into
price = []
if (choice == 'nothing'):
    print(b)
    quit()
else:
    print(c)
    if (c != items):
        print("Sorry, I don't know that item")
        print(a)
    if (a == 'yes'):
        input('what is it? > ')
    else:
         print(b)
         quit(all)

if so you are welcome, if not can you please explain a little more to me?

Related