I didn't want to post a new answer but instead make a comment, but alas, not enough reputation.
I just wanted to add to Daemon Painter's answer and say that the final total bill also isn't working as it is multiplying a dict by an int.
What could work is to initialize out of the loop the total variable as well a new total_cost variable, and place:
total_cost += total
Inside the while loop but outside the if conditions. That way the last line can just be:
print("The total price of your basket is: ", total_cost)
Edit: the code, working as intended:
price = {"Lemonade": 1.50, "Coke": 2.00, "Fanta": 1.00, "Water": 0.50}
shopping_basket = {}
print("Welcome to the online drink store!\nThese are the drinks we offer\n1. Lemonade: £1.50\n2. Coke: £2.00\n3. Fanta £1.00\n4. Water: £0.50")
buy_another_flag = 1
total_cost, total = 0, 0
while buy_another_flag != 0:
option = int(input("Which drink would you like to purchase?: "))
if option == 1:
qnty = int(input("Enter the quantity: "))
total = qnty * 1.50
print("The price is: " + str(total))
elif option == 2:
qnty = int(input("Enter the quantity: "))
total = qnty * 2.00
print("The price is: " + str(total))
elif option == 3:
qnty = int(input("Enter the quantity: "))
total = qnty * 1.00
print("The price is: " + str(total))
elif option == 4:
qnty = int(input("Enter the quantity: "))
total = qnty * 0.50
print("The price is: " + str(total))
total_cost += total
buy_another_flag = int(input("Would you like another item? enter Yes (1) or No (0):"))
print("The total price of your basket is: ", total_cost)
Answer from Márcio Coelho on Stack OverflowI didn't want to post a new answer but instead make a comment, but alas, not enough reputation.
I just wanted to add to Daemon Painter's answer and say that the final total bill also isn't working as it is multiplying a dict by an int.
What could work is to initialize out of the loop the total variable as well a new total_cost variable, and place:
total_cost += total
Inside the while loop but outside the if conditions. That way the last line can just be:
print("The total price of your basket is: ", total_cost)
Edit: the code, working as intended:
price = {"Lemonade": 1.50, "Coke": 2.00, "Fanta": 1.00, "Water": 0.50}
shopping_basket = {}
print("Welcome to the online drink store!\nThese are the drinks we offer\n1. Lemonade: £1.50\n2. Coke: £2.00\n3. Fanta £1.00\n4. Water: £0.50")
buy_another_flag = 1
total_cost, total = 0, 0
while buy_another_flag != 0:
option = int(input("Which drink would you like to purchase?: "))
if option == 1:
qnty = int(input("Enter the quantity: "))
total = qnty * 1.50
print("The price is: " + str(total))
elif option == 2:
qnty = int(input("Enter the quantity: "))
total = qnty * 2.00
print("The price is: " + str(total))
elif option == 3:
qnty = int(input("Enter the quantity: "))
total = qnty * 1.00
print("The price is: " + str(total))
elif option == 4:
qnty = int(input("Enter the quantity: "))
total = qnty * 0.50
print("The price is: " + str(total))
total_cost += total
buy_another_flag = int(input("Would you like another item? enter Yes (1) or No (0):"))
print("The total price of your basket is: ", total_cost)
You are looping indefinitely in the while loop.
Once you assigned option via User input here:
option = int(input("Which drink would you like to purchase?: "))
this condition is always fulfilled:
while option!= 0:
Here, you should not just print, but re-assign the option:
print("Would you like another item? enter Yes or No:")
something like this:
option = int(input("Would you like another item? enter Yes or No:"))
I hope this is pointing you in the right direction :)
(I've made assumptions on how you should have the code formatted in your original script. They might be wrong...)
Videos
First of all well done, this is not bad for a first program :D
Good
- Usage of functions
- Using correct datatypes (dictionary for example)
Some Improvements in random order
Read PEP8, the python style guide!
fix your indentation
it is standard to use
4spaces indentation in pythonPrettify your code for better readability
stock={'banana':6, 'apple':0, 'orange':32, 'pear':15,}this maybe subjective but that is worse to read then this
stock={ 'banana': 6, 'apple': 0, 'orange': 32, 'pear': 15 }uppercase can be simplified
def uppercase(x): return x[0].upper()+x[1:]There is a builtin function for this
>>> print("apple".capitalize()) AppleRemove tripple quotes if they are not neccesary
name=input('''What is your name? ''')There is no need to let this be over 2 lines, secondly maybe add a space for better UX
name=input('What is your name? ')Use
str.format()orf"strings"over old style formattingYou use old style formatting alot
"%s" % "somestring"it is better to use the new style formatting
"{}".format("somestring")See String format best practises
Use a
if __name__ == "__main__"guardIt will make your script importable while also being able to run from the CLI
if __name__ == '__main__': name=input('What is your name? ') print('Hi, {}, welcome to my fruit store. Here is the menu:'.format(name)) menu() ask_fruit(117)Avoid Magic numbers
money=117Why
117? Because numbers can't have an explanation it is called a magic numberInstead you can make it a
globalSTARTING_MONEY = 117Instead of empty
print()use\nPython doesn't really suit itself for recursion (recursion depth restrictions, memory consumption)
def ask_fruit(money): fruit=input('''What fruit do you want? ''') print() if fruit in stock: if stock[fruit]>0: ask_amount(fruit,money) else: print('''Sorry, %ss are out of stock '''%(fruit)) ask_fruit(money) else: print('''Sorry, we don\'t have that, look at the menu. ''') ask_fruit(money)Can be rewritten iterative
def ask_fruit(money): while True: fruit=input('What fruit do you want? ') print() if fruit in stock: if stock[fruit] > 0: ask_amount(fruit, money) else: print('Sorry, {}s are out of stock'.format(fruit)) continue else: print("Sorry, we don't have that, look at the menu.") continue
Good job!
I would personally prefer to use f-strings, which are more readable.
# from
print('Price: $%s'%(prices[fruit]))
# to
print(f'Price: ${prices[fruit]}')
Using newlines ('\n') would save you from using that many prints and make your code more readable.
Four spaces (instead of two) for indentation would also show your awareness of the PEP8 style conventions
Try/Except structure (catching errors of a particular type) is more preferable and is considered as Pythonic way (refer to https://docs.python-guide.org/).
Use dict.get(). refer to: https://docs.quantifiedcode.com/python-anti-patterns/correctness/not_using_get_to_return_a_default_value_from_a_dictionary.html