-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?
– Woss
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
– roger roger
And have you seen my answer? You no longer do all you need?
– Woss
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'
– roger roger
Yes, because there was nothing specifying that the exponent could be a fraction as well; just read, understand the code and adapt it.
– Woss
Roger, did you see that I made edits to my answer? Any questions remain?
– Woss
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.
– roger roger
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'
.– Woss
I get it. Grateful.
– roger roger