3

My goal is to carry out the instructions below:

Enter integer:

4

You entered: 4

4 squared is 16

And 4 cubed is 64 !!

Enter another integer: 5

4 + 5 is 9

4 * 5 is 20

Here is my code:

user_num = int(input('Enter integer:\n'))
    
print("You entered: ", user_num)
    
print(user_num, "squared is ", user_num*user_num)
    
print("And", user_num, "cubed is", user_num*user_num*user_num, "!!")
    
user_num2 = int(input("Enter another integer:\n"))
    
print(str(user_num) + str(user_num2), "is", user_num+user_num2)
    
print(str(user_num) * str(user_num2), "is", user_num*user_num2)

The problem is that the last two lines of my codes are not giving me what I want. I want the inputs for user_num and user_num2 to be printed in a non-concatenated way and without execution to read, for example, "4 + 3 is 7" and "4 * 3 is 12". Any help will be greatly appreciated. Thanks

6 Answers 6

4

Take advantage of f-strings so your print construction is easier to produce.

A corrected solution would look like the following:

user_num = int(input('Enter integer:\n'))

print(f"You entered: {user_num}")
print(f"{user_num} squared is {user_num**2}")
print(f"And {user_num} cubed is {user_num**3}!!")

user_num2 = int(input("Enter another integer:\n"))

print(f"{user_num} + {user_num2} is {user_num+user_num2}")
print(f"{user_num} * {user_num2} is {user_num*user_num2}")

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Anderson. This is really helpful.
2
user_num = int(input('Enter integer:\n'))
print("You entered: ", user_num)
print(user_num, "squared is ", user_num*user_num)
print("And", user_num, "cubed is", user_num * user_num * user_num, "!!")
user_num2 = int(input("Enter another integer:\n"))
print(str(user_num), "+", str(user_num2), "is", user_num+user_num2)
print(str(user_num), "*", str(user_num2), "is", user_num*user_num2)

When you want to print the operators, you have to put them in quotation marks

Comments

2

You could do it like this:

print(str(user_num), "+", str(user_num2), "is", user_num+user_num2)

print(str(user_num), "*", str(user_num2), "is", user_num*user_num2)

Notice how i replaced + and * in your code to the string "+" and "*" respectively.

1 Comment

Thank you very much. This is really helpful.
2

The code you wrote seems good but you will get errors at last two lines cause you are trying to add and multiply two strings which are not possible. So you can do this

user_num = int(input('Enter integer:\n'))

print("You entered: ", user_num)

print(user_num, "squared is ", user_num*user_num)

print("And", user_num, "cubed is", user_num*user_num*user_num, "!!")

user_num2 = int(input("Enter another integer:\n"))

print(user_num, '+', user_num2, "is", int(user_num)+int(user_num2))

print(user_num, '*', user_num2, "is", int(user_num)*int(user_num2))

also editing the last two lines

print(str(user_num), '+' ,str(user_num2), "is", user_num+user_num2)

print(str(user_num), '*' ,str(user_num2), "is", user_num*user_num2)

1 Comment

Thank you very much Sasidhar. It worked. This is really helpful.
1

Use f-strings.

Here is my code:

unum1 = int(input('Enter integer:\n'))

print(f"You entered: {unum1}")
print(f"{unum1} squared is {unum1**2}")
print(f"And {unum1} cubed is {unum1**3}!!")

unum2 = int(input("Enter another integer:\n"))

print(f"{unum1} + {unum2} is {unum1+unum2}")
print(f"{unum1} * {unum2} is {unum1*unum2}")

1 Comment

Thank you very much Yourson. This is really helpful.
-1
user_num = int(input('Enter integer:\n'))

print('You entered:',user_num)

print(user_num, 'squared is', user_num * user_num)

print('And', user_num, 'cubed is', user_num * user_num * user_num,'!!')

user_num2 = int(input('Enter another integer: \n'))

print(user_num, '+', user_num2, 'is', user_num + user_num2)

print(user_num, '*', user_num2, 'is', user_num * user_num2)

1 Comment

This seems to be a poorly formatted copy of an existing answer.

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.