#πŸ”’ How do I exponentiate the numbers and sum then after that

32 messages Β· Page 1 of 1 (latest)

barren oak
#

I need just to exponentiate the numbers by the last one, for example if I insert 42 the number 4 must be exponentiated by the number 2, if 63 the number 6 by 3 and etc... after that I need to sum all of the results, to do this a tried to separate the values in two different lists but nothing that I do now works properly, please help me find out what do I need to implement or even if I can proceed to get the results I want with my code.

#Get the quantity of terms
n = int(input("Quantity of terms: "))

#List to store the expressions.
lista_n = []

#Loop to add the terms in the list.
for c in range(n):
t = int(input("Terms: "))
lista_n.append(t)

#Separated list of the exponents (the last value of each item).
exponents = [str(item)[-1] for item in lista]

#Separated list of the rest of the numbers.
numbers = [str(item)[:-1] for item in lista]

#Print to see if its all correct (It is until now)
print(exponents)
print(numbers)

bitter cobaltBOT
#

@barren oak

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

flat oxide
barren oak
flat oxide
#

Yes, exactly

boreal birch
#

to do exponents in python, you use **, so 8^2 is 8**2. For this problem, you could probably use list comprehension. something like:

results = [numbers[i]**exponents[i] for i in range(len(exponents))]

(i hope)

flat oxide
flat oxide
#

you want to basically take each number in numbers, and put it to the power of the number in exponents in the same index position

#

so numbers[0] ** exponents[0], then numbers[1] ** exponents[1], and so on

#

do you see a pattern here?

barren oak
flat oxide
#

ahh yes, so currently you've converted all your numbers to str so you were able to slice the first and last numbers

#

but if you want to do that math, you'll need to convert back to int

barren oak
barren oak
flat oxide
#

the better way to do this would be using zip()

#

but len and range would work otherwise

barren oak
#

I've already tried zip also like this

#

example = zip(numbers, exponents)
for expressions in example:
print(expressions)

#

And it returns me the right values

flat oxide
#

A neat thing you can do with zip and for loops is you can use multiple variables

#
for num, exp in zip(numbers, exponents):
barren oak
flat oxide
#

yes

#

you can't do exponents with strings

barren oak
#

Appreciate it, I'll try it right now

barren oak
#

I did it, THANK YOU

flat oxide
bitter cobaltBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.