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.
4 Antworten
+ 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)
+ 1
@Rik Wittkopp @NEZ
followed you already
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