Is there any inbuilt function or any modules for this if else statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there any inbuilt function or any modules for this if else statement

num = 1.0 if num % 1 == 0: int(num) else: None print(num) Output:- 1 https://sololearn.com/compiler-playground/cu124eMkXjgT/?ref=app

29th Dec 2023, 5:07 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
8 Answers
+ 6
math.fmod() will be recommended to perform modulo operations on float values. If you’re using a negative operand, then you may see different results between math.fmod(x, y) and x % y.
29th Dec 2023, 7:53 AM
JaScript
JaScript - avatar
+ 4
30th Dec 2023, 12:48 PM
Louis
Louis - avatar
+ 3
Thank you JaScript
29th Dec 2023, 10:10 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
+ 2
P A Arrchith Iyer , I like your program's output, and it didn't take long, even though the code comment said it might. However, I agree with the general consensus that the problem you think you have that you're trying to solve with that complicated input validation is a non-problem, because a float is already the perfect type for those math routines. I refactored that section like so, and it worked fine. # Input Validation while True: try: num = float(input()) except ValueError: print("Input must be a number.") else: break (Of course, on Sololearn, the user doesn't get a second chance at input, but the loop will work in an IDE.)
29th Dec 2023, 1:09 PM
Rain
Rain - avatar
+ 1
At first I was thinking about ternary conditional expressions... num = 1.23 num = int(num) if num%1==0 else None print(num) # Nothing printed But after inspecting your code in # Input Validation, else: None doesn't have any effect. You code simply check if "num" is a number not a string. Even it is a number it won't convert a number with decimal point into an integer. It only converts to integer if the decimal digits are all zero.
29th Dec 2023, 11:07 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
P A Arrchith Iyer yes, I agree with Wong Hei Ming . I'm unclear what that check is for. Is it to convert whole number float to int? 1.0 to 1 2.0 to 2 What's the advantage in that? also there is no need for the else: None
29th Dec 2023, 11:17 AM
Bob_Li
Bob_Li - avatar
+ 1
Thank you Rain
29th Dec 2023, 1:50 PM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
0
Thank you Mirielle
29th Dec 2023, 6:40 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar