Reputation: 517
I have to write a simple program in python 3.6 that takes the user input and computes the value of n+n*n+n*n*n+n*n*n*n
.
So if the user enters 7, then the following should be printed to the console 7+7*7+7*7*7+7*7*7*7 = 2800
.
How do i accomplish this with the print function?
So far i have tried the following:
input_int = int(input("Please enter a value: "))
result_int = input_int + input_int * input_int + input_int * input_int * input_int + input_int * input_int * input_int * input_int
print(input_int + input_int * input_int + input_int * input_int * input_int + input_int * input_int * input_int * input_int, " = ", result_int)
and doesn't give me what i want.
Upvotes: 2
Views: 258
Reputation:
After reading the latest comments, I think you are looking for something like this:
x = 7
result = 7 + (7 * 7) + (7 * 7 * 7) + (7 * 7 * 7 * 7)
print(f'{x} + {x} * {x} + {x} * {x} * {x} + {x} * {x} * {x} * {x} = {result}')
Output
7 + 7 * 7 + 7 * 7 * 7 + 7 * 7 * 7 * 7 = 2800
Python Documentation: Formatted String Literals (f-strings)
Upvotes: 1
Reputation: 605
If the number of iteration is fixed to 4 then you can iterate from 1 to 5 or if your iteration is dynamic you can replace 5 with your variable.
input_int = int(input("Please enter a value: "))
out = 0
string = ''
for i in range(1,5):
string += (str(input_int)+"*")*i+"+"
out += pow(input_int, i)
string = string.replace("*+", " + ")[:-2]+" = "
print(string+str(out))
Explain:
for is iterating from 1 to 5 which results in 4 times iteration.
string += (str(input_int)+"")i+"+" which will create a string display like 7 + 77+ 777*+
Here extra '*' and '+' sign need to be removed.
out += pow(input_int, i)
Pow is the default python function to calculate the square of a number like pow(7,2)
Replace extra '*' and '+' with replace
and add '=' at the end.
Upvotes: 1
Reputation: 27588
Just a little math.
>>> n = 7
>>> (n**5 - 1) // (n - 1) - 1
2800
Upvotes: 0
Reputation: 51
input_int = int(input("Please enter a value: "))
result_int = input_int + input_int * input_int + input_int * input_int * input_int + input_int * input_int * input_int * input_int
input_string = f'{input_int} + {input_int} * {input_int} + {input_int} * {input_int} + {input_int} * {input_int} + {input_int} * {input_int} + {input_int} = {result_int}'
print(input_string)
Upvotes: 2
Reputation: 43
Considering your result is already stored in result_int, and the 'n' is stored in input_int.
print(input_int + input_int * input_int + input_int * input_int * input_int + input_int * input_int * input_int * input_int, " = ",result_int)
This wouldn't work, as you are directly printing the value.
You need to use something like:
print(input_int + '+' + input_int + '*' + input_int + '+' + input_int + '*' + input_int + '*' + input_int + '+' + input_int + '*' + input_int + '*' + input_int + '*' + input_int, " = ",result_int)
This would give the desired output.
Upvotes: 1
Reputation: 11893
Here is a bit of help as this seems like a H/W assignment.
You need to convert the integers into strings and handle the math symbols as strings... So something like this...
In [2]: my_int = 7
In [3]: # convert to a string...
In [4]: my_int = str(my_int)
In [5]: print(my_int,'+',my_int,'*')
7 + 7 *
Upvotes: 1