I have a piece of code that will calculate the simplified version of a fraction:
def simp_frac[num,den]:
smallest = min[num, den]
for i in range[smallest, 1 , -1]:
if num%i == 0 and den%i == 0:
print [str[num/i] + "/" + str[den/i]]
break
where print[simp_frac[12,4]]
gives me the output I expect, 3/1
.
What I want to do is modify the function so that a user can input a fraction written in the form a/b
, but I cant figure out what type of variable to use and how
to "feed" my function the user input fraction in two separate values, the numerator and denominator.
asked Apr 14, 2015 at 5:18
2
You can use the input[]
built-in to prompt the user to enter values. On Python 2.x, use raw_input[]
.
Instead of printing your result, simply return it.
def simp_frac[num,den]:
smallest = min[num, den]
for i in range[smallest, 1 , -1]:
if num%i == 0 and den%i == 0:
return '{}/{}'.format[num/i,den/i]
# break - not needed
user_input = input['Please enter a fraction: ']
num, den = user_input.split['/']
print['Your result is: {}'.format[simp_frac[int[num], int[den]]]]
We need to convert the input to integers as all input will be submitted as strings.
answered Apr 14, 2015 at 5:27
Burhan KhalidBurhan Khalid
164k18 gold badges238 silver badges276 bronze badges
num, den = raw_input[].split['/']
simp_frac[float[num], float[den]]
answered Apr 14, 2015 at 5:27
Barun SharmaBarun Sharma
1,4022 gold badges14 silver badges20 bronze badges
You can ask a user for input data as follows:
user_input = input["Provide fraction as num/dem [e.g. 1/3]: "]
# in python 2.7: user_input = raw_input["Provide fraction as num/dem [e.g. 1/3]: "]
num,den = map[float, user_input.split['/']]
simp_frac[num,den]
answered Apr 14, 2015 at 5:27
MarcinMarcin
182k11 gold badges156 silver badges219 bronze badges
4
Simple play on string split method and splat operator.
def simp_frac_string[s]:
return simp_frac[*[int[i] for i in s.split['/']]]
answered Apr 14, 2015 at 5:28
Łukasz RogalskiŁukasz Rogalski
20.8k8 gold badges54 silver badges89 bronze badges
I think this type of code helps for calculations.
from fractions import Fraction
x = raw_input['Enter a Fraction: ']
y = Fraction[x]
print[y]
z = y+1.5
print['output:', z]
raw_input takes any input as String and Fraction converts it to Float
Sample Input:
Enter a Fraction: 3/5
output: 2.1
answered Feb 26, 2020 at 12:04
debaonline4udebaonline4u
4,6794 gold badges34 silver badges41 bronze badges
You can use "Fraction Module in python" as, from fractions import Fraction frac = Fraction[raw_input[]]
answered Jul 13, 2021 at 6:10
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.
''' 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]
//stackoverflow.com/questions/29619656/i-want-to-input-a-fraction-into-a-function-in-python
@Rik Wittkopp @NEZ followed you already
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