how do we input fraction in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how do we input fraction in python?

You see, I'm working on a project called "Area of circle", we all know that pi is equal to 3.142, 3.14 and 22/7. Well, I want to let user input either float or fraction. How can I do it without downloading things?Thanks.

17th May 2021, 2:06 AM
SOON YEE FENG Moe
4 Answers
+ 1
''' Create a string input. Process that input through an if/else filter to choose which input style to use. I used a function to do this so you can use the result throughout the code''' pi_choice = input() #diameter = int(input()) def convert(x): if '/' not in x: return float(x) else: fract = x.split('/') return int(fract[0])/int(fract[1]) print(convert(pi_choice)) #print(convert(pi_choice) * diameter)
17th May 2021, 9:18 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
@Rik Wittkopp @NEZ followed you already
18th May 2021, 12:26 AM
SOON YEE FENG Moe
0
you just need to figure out what criteria is required to use either 3.14 or 22/7. you can simply use if-else statement if you got the criteria
17th May 2021, 2:21 AM
Rellot's screwdriver
Rellot's screwdriver - avatar