Raising fraction to power

Asked

Viewed 194 times

-1

I need to raise a number to a certain expoente, but I don’t know how to do it in case frações. For example, I know that to raise 2 to terceira potência, I must do 2**3. But if I want to raise 1/2 to the third power, as would be?

My code is just below.

The way I’m doing, I get the following error message: ValueError: could not convert string to float: '1/2'

I would like the user to enter a number in the form of a fraction (1/2) and not in decimal form 0.5.

base = float(input('Digite a base: '))
exp = int(input('Digite o expoente: '))

result = base**exp

print('a base ', base, ' elevada ao expoente ', exp, ' é igual a: ', result)
  • Roger, what is your intention with this issue by adding a new code to the question?

  • I intend to treat all cases from a high base to an exponent, fractional or not. For example, 2 2, or 2 (3/2). Executing the changed code gives this error: File "main.py", line 8 if (base_array.find('/')=-1): Syntaxerror: invalid syntax Keyboardinterrupt

  • 1

    And have you seen my answer? You no longer do all you need?

  • I was trying here, but when I typed the fractional exponent gave the following error. Hence, I am trying another solution. Look, the error: Traceback (Most recent call last): File "main.py", line 4, in <module> Exp = int('Exponent: ')) Valueerror: invalid literal for int() with base 10: '3/7'

  • 1

    Yes, because there was nothing specifying that the exponent could be a fraction as well; just read, understand the code and adapt it.

  • 1

    Roger, did you see that I made edits to my answer? Any questions remain?

  • Hello, it’s working as I want it now. I just don’t understand why you made map(int, base.split('/', 1)) and then assign "1" to the denominator.

  • 1

    The map is to convert the input to whole. If type '1/2' he returns (1, 2) as integers. The denominator equal to 1 is when a fraction is not typed in the input, for example '4'.

  • I get it. Grateful.

Show 4 more comments

2 answers

7


You can use fractions to deal with fractions:

from fractions import Fraction

base = Fraction(1, 2)
exponent = 3

result = base**exponent

print(result)  # 1/8

If the idea is to accept as user input values in numerator/denominator format, you will need to treat in order to generate your final fraction.

base = input('Base: ')
exponent = input('Expoente: ')

if '/' in base:
    numerator, denominator = map(int, base.split('/', 1))
else:
    numerator = int(base)
    denominator = 1

base = Fraction(numerator, denominator)

if '/' in exponent:
    numerator, denominator = map(int, exponent.split('/', 1))
else:
    numerator = int(exponent)
    denominator = 1

exponent = Fraction(numerator, denominator)

print(base**exponent)

0

It follows a great solution:

base = input('Digite a base: ')
exp = int(input('Digite o expoente: '))

m_array = base.split("/")
base = int(m_array[0])/int(m_array[1])

result = base**exp

print('a base ', m_array[0] + "/" + m_array[1], 'elevada ao expoente ', exp, ' é igual a: ', result)
  • It worked. Only I want the result to come in the form of a fraction with a numbered and denominator. That is, in result, I must have 1/2 and not 0.5. Grateful.

  • @rogerroger ready

  • Giving error: Traceback (Most recent call last): File "1_potencias.py", line 12, in <module> Exp = int('Enter exponent: ') Valueerror: invalid literal for int() with base 10: '1/2'

  • 1

    @rogerroger, this error happens when you do not type the base and press enter. To solve this problem you will have to check if the data was typed and if they were typed correctly.

  • You’re right. As I intend that the program also interprets denominator "1", ie 4/1, for example, I will try to treat this case with an if. Thankful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.