Could you please tell me what's a better way of writing this Python code? Kaleidoscopes exercise.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Could you please tell me what's a better way of writing this Python code? Kaleidoscopes exercise..

So as you can see I'm a total beginner.. one of my exercises that I worked on is Kaleidoscopes. Here if a customer buys more than one, they get a 10% discount on all of them, otherwise they pay whole price. The price for each one is 5.00. They also pay tax which is 7%.. I have done it correctly and we can get the desired output, but I feel that the code I wrote could have been written better or in a shorter way. Can you suggest any modifications please? Here's the code: kaleidoscopes_bought=int(input('Number of Kaleidoscopes: ')) total_price=(5*kaleidoscopes_bought) if kaleidoscopes_bought>1: discount=(total_price*10)/100 new_price=total_price-discount tax=(7*new_price)/100 whole_payment=new_price+tax print("discounted price + tax:

quot;,float(whole_payment)) else: whole_price=total_price+7*total_price/100 print("whole price+tax:
quot;,float(whole_price))

5th Aug 2023, 4:12 PM
Nour Mohamed
Nour Mohamed - avatar
5 Answers
+ 4
Nour Mohamed Something better suited for posts. 1. Your spacing between your signs. For example (total_price*10)/100 is different from new_price + tax. This is probably the biggest issue here. 2. Dont abbreviate variable names it just makes it harder to read (in some cases you can but here no one knows what ks is) . Also your abbreviating and non abbreivating in some cases. 3. Not really a big thing here but you put a space for the comma on the discounted price + tax and your whole price + tax you dont space it.
5th Aug 2023, 4:38 PM
Junior
Junior - avatar
+ 4
my code for that i = int(input()) x = 5 #price d = discount = 0.90 tax = 1.07 print(round((i * x * tax * d if i > 1 else x * tax),2)) but your code as a beginner is very good., important is that you understand that your code is doing.
5th Aug 2023, 5:00 PM
Angela
Angela - avatar
+ 4
Nour Mohamed , your code as it is may run properly in playground. but if you like to verify the result with different test cases by using *code coach* the code will fail. the reason is: > a string is used as a user prompt that is given for the input procedure > a string that is given for the output of the result for code coach exercises only required and demanded output is accepted. # sample code: using an f - string for calculation and for rounding: inp = int(input()) if inp == 1: print(f'{inp * 5 * 1.07:.2f}') else: print(f'{inp * 0.90 * 5 * 1.07:.2f}')
5th Aug 2023, 7:13 PM
Lothar
Lothar - avatar
+ 1
@Junior Thank you for your suggestion I've edited the code accordingly now.. @Angela Thank you so much that was very helpful. I'm just not confident enough to use a straight-to-the-point code like the one you're showing yet.. but I'll definitely keep practicing using this. Thanks again!
5th Aug 2023, 5:52 PM
Nour Mohamed
Nour Mohamed - avatar
0
Certainly! I would be happy to suggest some modifications to make your Python code for the Kaleidoscopes exercise more concise and efficient. Here's an improved version of your code: ```python kaleidoscopes_bought = int(input('Number of Kaleidoscopes: ')) total_price = 5 * kaleidoscopes_bought if kaleidoscopes_bought > 1: discount = total_price * 0.1 total_price -= discount tax = total_price * 0.07 total_price += tax print(f"Total price: ${total_price:.2f}") ``` Here's an explanation of the modifications made: 1. Removed unnecessary parentheses: The unnecessary parentheses in the calculation of `discount` and `tax` were removed to simplify the code. 2. Use of floating-point numbers for percentages: Instead of dividing by 100, we can directly multiply by the decimal value of the percentage. For example, 10% is equivalent to 0.1. 3. Combined tax calculation: Instead of calculating tax separately, the tax is now calculated as a percentage of the total price and added directly to the `total_price`
6th Aug 2023, 3:33 PM
Super sonic speed
Super sonic speed - avatar