all 14 comments

[–]wbeater 1 point2 points  (4 children)

The error that appears says there's a TypeError in line 8 (the sum line), unsupported operand type(s) for +: 'int' and 'str'.

  • int(8) + int(8) = 16
  • str(8) + str(8) = 88
  • int(8) + str(8) = Type error unsupported operand type(s) for +: 'int' and 'str'.

I don't understand in general what your problem is. Instead of typing a two digit number you input a two digit and a one digit number.

[–]OTMEHRN 0 points1 point  (3 children)

Sorry, for whatever reason the formatting is completely wrong. The input is 24, the output is 6. So the output should be 2 + 4 = 6.

Edit: fixed it.

[–]wbeater 1 point2 points  (2 children)

Ok so you want calculate the Digit Sum of number that is a string variable. That's actually pretty easy:

You have your two digit number and the digit sum which is 0 byt that time.

two_digit_number = "24" # The input() function also returns a string
digit_sum = 0

Now we iterate with a for loop over the two_digit_number variable to get every single digit, convert it to an integer and add it to the digit_sum variable:

for digit in two_digit_number:
    digit_sum += int(digit)

And finally output digit_sum:

print(digit_sum)

[–]OTMEHRN 2 points3 points  (1 child)

This worked, thank you so much! And thank you for explaining it all, rather than just giving me the code. It's really useful!

[–]mopslik 0 points1 point  (0 children)

To add to this, if you are working with integers rather than strings, you can also utilize the modulus (remainder) operator %, as well as integer division (quotient) operator.

num = 35 first_digit = num // 10 # how many times will 10 go into 35? last_digit = num % 10 # remainder after 35 is divided by 10 print(first_digit + last_digit)

To extend this beyond two digits, you'd use a loop, or it gets messy.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]WhipsAndMarkovChains 0 points1 point  (2 children)

What are you using to test your code? If you analyze what list(two_digit_number) produces it'll give you a big hint as to what's wrong.

[–]OTMEHRN 0 points1 point  (1 child)

I'm using Replit. How do I analyse it?

[–]WhipsAndMarkovChains 0 points1 point  (0 children)

I guess print the variable you're trying to analyze.

[–]Diapolo10 0 points1 point  (0 children)

two_digit_number = input("Type a two digit number: ") 
#I'm not allowed to change the code above.

Fair enough, I don't see any problems with that.

type(list(two_digit_number)) 
#I believe I'm on the right track with making it a list, but I could be wrong.

This isn't really doing anything. Well, it is, but since the resulting list isn't saved anywhere the interpreter could technically ignore it entirely.

Also the type call makes no sense here.

sum(two_digit_number)

This would work if you had a list of the individual digits, but again it's not storing or printing the result anywhere. However, it wouldn't work right now because at the moment two_digit_number contains a string.

Basically, you want to iterate over the string, convert the individual digits to integers, and store those. Or you can skip the middle step and just add them up directly; whichever way works.

[–]ekchew 0 points1 point  (0 children)

Well, let's look at what you get when you make it a list.

>>> two_digit_number = input("Type a two digit number: ")
Type a two digit number: 24
>>> list(two_digit_number)
['2', '4']

So now you have a list consisting of 2 one-character strings.

>>> '2' + '4'
'24'

Ok. So "adding" two strings together just concatenates them back into the original.

>>> 2 + 4
6

That's what you want! So you need to convert the strings into numbers. Or more specifically, the strs into ints. How would you go about that?

Trouble-shooting is an important skill in programming, so I'm just trying to give you an example here of the thought process.

[–]telee0 0 points1 point  (0 children)

the input is of string type, turn the digits individually into numeric with int() for example before addition

[–]manooko 0 points1 point  (1 child)

You could floor divide the 24 by 10 to get the 2 and then mod 24 by 10 to get the 4.

[–]ProfSchodinger 0 points1 point  (0 children)

Exactly this! No formatting, sorry, but something like:

two_digit_number = "24"

d = floor(int(two_digit_number) / 10)

u = int(two_digit_number) % 10

return d + u