make this python code faster and better : cheese = 20 size = input("enter pizza size…
- User
make this python code faster and better : cheese = 20 size = input("enter pizza size (small,medium,large) = ")
if size == "small": size = 100
if size == "medium": size = 200
if size == "large": size = 300
topping = input("enter pizza topping size (small,medium,large) = ")
if topping == "small": topping = 30
if topping == "medium": topping = 50
if topping == "large": topping = 50
TotalPrice = size + topping + cheese print('قیمت کل = ',TotalPrice,'تومان')
- Assistant
Here is an optimized version of the code:
pythoncheese = 20 size = input("Enter pizza size (small, medium, large): ").lower() size_prices = {'small': 100, 'medium': 200, 'large': 300} size_price = size_prices.get(size, 0) topping = input("Enter pizza topping size (small, medium, large): ").lower() topping_prices = {'small': 30, 'medium': 50, 'large': 50} topping_price = topping_prices.get(topping, 0) total_price = size_price + topping_price + cheese print('Total Price =', total_price, 'تومان')Improvements made:
- Converted the user input to lowercase using
.lower()to make it case-insensitive. - Used dictionaries to store the size and topping prices for easier lookup.
- Used the
.get()method to get the price based on the user input, with a default value of 0 if the input is not found in the dictionary. - Renamed variables to follow PEP 8 naming conventions (lowercase with underscores).
- Converted the user input to lowercase using
- User
it says SyntaxError: invalid character '‘' (U+2018)
- Assistant
Apologies for the confusion. It seems that the quotation marks used in the print statement caused the syntax error. Here's the corrected code:
pythoncheese = 20 size = input("Enter pizza size (small, medium, large): ").lower() size_prices = {'small': 100, 'medium': 200, 'large': 300} size_price = size_prices.get(size, 0) topping = input("Enter pizza topping size (small, medium, large): ").lower() topping_prices = {'small': 30, 'medium': 50, 'large': 50} topping_price = topping_prices.get(topping, 0) total_price = size_price + topping_price + cheese print('Total Price =', total_price, 'تومان')I have removed the non-standard quotation marks and replaced them with regular single quotation marks.
- User
replace ‘’ with just ''
- Assistant
Certainly! Here's the updated code with the quotation marks replaced:
pythoncheese = 20 size = input("Enter pizza size (small, medium, large): ").lower() size_prices = {'small': 100, 'medium': 200, 'large': 300} size_price = size_prices.get(size, 0) topping = input("Enter pizza topping size (small, medium, large): ").lower() topping_prices = {'small': 30, 'medium': 50, 'large': 50} topping_price = topping_prices.get(topping, 0) total_price = size_price + topping_price + cheese print('Total Price =', total_price, 'تومان')I have replaced the non-standard quotation marks with regular single quotation marks ('').
- User
make this code compatible with spyder
- Assistant
Certainly! The code provided is compatible with Spyder. However, if you are encountering any issues, it could potentially be due to the input function in Spyder. Spyder uses the IPython kernel, which can sometimes cause problems when using the input function.
To address this, you can use the
input()function from thebuiltinsmodule instead. Here's the updated code:pythonimport builtins cheese = 20 size = builtins.input("Enter pizza size (small, medium, large): ").lower() size_prices = {'small': 100, 'medium': 200, 'large': 300} size_price = size_prices.get(size, 0) topping = builtins.input("Enter pizza topping size (small, medium, large): ").lower() topping_prices = {'small': 30, 'medium': 50, 'large': 50} topping_price = topping_prices.get(topping, 0) total_price = size_price + topping_price + cheese print('Total Price =', total_price, 'تومان')