Kaleidoscopes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Kaleidoscopes

So this it my code...but is there a much shorter way to the task? #my code order = int(input()) price = 5.00 total = order*price if order > 1: disc = total*10/100 tax = (total - disc)*7/100 answer = total - disc + tax print(round(answer , 2)) elif order == 1: tax = total*7/100 print(total + tax)

2nd Sep 2021, 5:33 AM
Ervan
Ervan - avatar
6 Answers
+ 6
Haris , it would be better if you start your own question, because the current discussion started one year ago. hence people may not be aware of your issue. also it is not clear if the task description from this thread is identical of what you want to do.
3rd Jan 2023, 3:55 PM
Lothar
Lothar - avatar
+ 6
Haris , don't worry. for this case just update your post with some information what you want to achieve with your code.
3rd Jan 2023, 4:23 PM
Lothar
Lothar - avatar
+ 5
Ervan , you can have a much shorter way than you have done it. but "short" should not be the criteria for coding. BTW i like doing one-liners, but mostly for challenge myself. i have never done it in real applications, that has to be debugged, modified, maintained,... following the Zen of Python means: Simple is better than complex. Flat is better than nested. Readability counts. here is a file with 2 versions: https://code.sololearn.com/cju8aJgSYP5k/?ref=app
2nd Sep 2021, 8:31 AM
Lothar
Lothar - avatar
+ 3
Hi Ervan! Of course, you can use ternary operator instead and give direct values without declaring additional variables. Syntax for ternary operator: print(output1 if(condition) else output2) Here it is the shorter version order = int(input()) print(round(order*5*1.07,2) if order ==1 else round(order*5*1.07*0.9,2))
2nd Sep 2021, 6:22 AM
Python Learner
Python Learner - avatar
+ 1
Lothar I don't know much about that, sorry!
3rd Jan 2023, 3:56 PM
Haris
0
[Solved] Java version: import java.io.*; import java.util.Scanner; import java.lang.Math; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double price = 5.00; int nbreBuy = scan.nextInt(); double discount; double tax,total,ntotal; if(nbreBuy<=1){ tax = price*0.07; total = price+tax; System.out.println(total); }else{ ntotal = nbreBuy*price; discount = ntotal*0.10; total = ntotal-discount; tax = total*0.07; double t = total+tax; System.out.format("%.2f",t); } } }
25th Jan 2022, 1:01 AM
Moussa Diallo
Moussa Diallo - avatar