Adding one to the multiplying or exponenting etc. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Adding one to the multiplying or exponenting etc.

I want phyton to add 1 by itself to my multiplying (sorry, can't describe my problem) let's say x = 2 x **= 2 should output 4 as 2^2 is 4 i want it to make it x **= 3 by itself if possible can it stop in a point, for example 28( x **= 28 and how could i add them together like x = 2 print(x **= 2 + x **= 3 + x** = 4) Basically this is what i want my code to do for me. Any help is valued, thank you for your time

6th Feb 2017, 3:11 AM
Magnum Opus
Magnum Opus - avatar
6 Answers
+ 7
x = 2; y = 2; sum = 0; while y <= 28 : x **= y++ sum += x print(sum) # Not the best at python # But yeah lol # I might have made errors on the syntax tho # Take note that value of x changes each loop # Not sure if you want that to happen # If you don't, just replace x with 2.
6th Feb 2017, 3:45 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
I cross-checked things on Code Playground and found out that some of the syntax may not have been compatible with Python. (I main C++) I've made another version of the code, please try if it works. x = 2; y = 2; sum = 0; while y <= 28 : sum += (x ** y) y += 1 print(sum) I realise that I'm doing semicolons in variable declarations. This is a C++ thing and is totally unneeded in Python. I just... feel comfortable doing it and apparently it doesn't cause an error lol.
6th Feb 2017, 4:13 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
@Magnum Opus print("Your result is " + str(sum))
6th Feb 2017, 4:24 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
The error might be due to the resulting value exceeding integer upper limit. Sum is just a variable, while is a type of loop. You may refer to the Python Tutorial here on SoloLearn.
6th Feb 2017, 4:09 AM
Hatsy Rei
Hatsy Rei - avatar
0
I wrote it down exactly as you said, only changed 28 with 64, it gave some syntax error at first but now it doesn't, but it doesn't print any result neither :( where can i learn sum and while and why did you use ; and : in some lines, thank you
6th Feb 2017, 3:59 AM
Magnum Opus
Magnum Opus - avatar
0
This one works! Im going to school now, how do we add a name to it like Your result is 536870908
6th Feb 2017, 4:23 AM
Magnum Opus
Magnum Opus - avatar