Is there a shorter and better way? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there a shorter and better way?

''' First of all, pardon my English, it isn´t my first language. This code is intended to be a function that takes part in anothe programme of mine. If you want to check it out, this is the link https://code.sololearn.com/cA16A16a23A4/# This code asks for a floating point number such as 1.3421, 312.122 or 0.1111 etc, then it gives you the same number but 1e-("number of decimal places") subtracted. For example: ---------------------- input = 123.3455 output = 123.3454 ---------------------- input = 0.3 output = 0.2 ------------------- This is its final version: def func(input): num_b = list(str(intput)) ceros = len(num_b) - (num_b.index(".")+1) minus = 1/(10**ceros) output = round (input - minus,ceros) return output Greetings from Mexico. ''' num = input ("Enter a float \n") in_put = float (num) assert not(in_put-int(in_put) == 0),"Invalid input. {} isn´t a number \n with digits after the floating point".format(num) num_b = list(num) ceros = len(num_b) - (num_b.index(".")+1) minus = 1/(10**ceros) result = round (in_put - minus,ceros) print ("This is your number or minuend {} \n This is its subtrahend {}".format(in_put,minus)) print ("This is the difference {} ".format(result))

21st Dec 2020, 6:38 AM
Tecolote Come Elote
Tecolote Come Elote - avatar
1 Answer
+ 2
It may be more shorter, but I try. Hope you like the code's line https://code.sololearn.com/c3A95qi3667u/?ref=app
21st Dec 2020, 1:18 PM
LONEWOLF
LONEWOLF - avatar