2

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.

7
  • What have you tried so far? Commented Sep 20, 2020 at 3:26
  • Updated my post Commented Sep 20, 2020 at 3:27
  • You can use convert the input_int to string, and use + to concatenate them. Commented Sep 20, 2020 at 3:28
  • Please show more context. Where does input_int come from? Where is your calculation for result_int? Commented Sep 20, 2020 at 3:29
  • Anyway, of course it doesn't give you what you want - there is nothing in your code, for example, that attempts to produce + or * symbols in the output. Think through the steps of the problem logically. Commented Sep 20, 2020 at 3:30

6 Answers 6

2
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)
Sign up to request clarification or add additional context in comments.

Comments

1

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 *

Comments

1

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.

2 Comments

Or can we use a for loop to print this iteratively?
Yes, we can use the for loop for it also.
1

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.

Comments

1

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)

Comments

0

Just a little math.

>>> n = 7
>>> (n**5 - 1) // (n - 1) - 1
2800

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.