-3

This is the code I’ve wrote as I’m doing the 100 day course.

 number = input(“write a two digit number\n”) a = number[0] b = number[1] result = int(a) + int(b) print(result) 

I am trying to get the end script to say the sum and the result instead of just displaying the result.

I.e if the two digit number is 32 I want to display: 3 + 2 = 5.

Any help would be appreciated.

I have tried lots of things but I am extremely new to this and cannot figure it out.

0

1 Answer 1

0
number = input("write a two digit number \n") a = number[0] b = number[1] result = int(a) + int(b) print(f"{a} + {b} = {result}") 

You need to add the variables to the print function. This method uses f-strings, which I don't think you probably wouldn't have come across just yet.

Another simpler way of doing this:

number = input("write a two digit number \n") a = number[0] b = number[1] result = int(a) + int(b) print(a + " + " + b + " = " + str(result)) 

You can concatenate strings in the print() function, but you can't concatenate two different data types, thus turning result into str().

Hope this helps.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.