0

I am attempting to take two-digit integers representing day-of-month, split the digits into single digits by taking each character in the single digit and adding them together to form a new number.

e.g. If the value for day was an integer 29, then the program would turn that into strings and split them into '2' and '9'. The program would then turn 2 and 9 into integers and add them together to equal 11. Since this is still a double digit number, the program would loop and 1 and 1 would be added together and the final value that would print would be 2. According to the code below(mostly the last ~5 lines), if I enter day=29, then the final answer I keep getting is 4 which is incorrect. Can someone help me fix this:

Note someone mentioned that I didn't re-enter dayStringSum and I accidentally deleted their post am not sure what that means at all.

 dayString = str(int(day))
# Turns value day into int
dayStringA = int(str(dayString[0]))
# If day=29 then this variable represents the 2...
dayStringB = int(str(dayString[1]))
# ...and this represents the 9
dayStringSum = (dayStringA + dayStringA)
while(dayStringSum >=10):
    dayStringA = int(str(dayStringSum[0]))
# Since daystringsum is now 11, this code changes the value of daystringA into a new value of 1, likewise for below.
    dayStringB = int(str(dayStringSum[1]))

print(dayStringSum)
smci
  • 32,567
  • 20
  • 113
  • 146
SeesSound
  • 503
  • 4
  • 13
  • 24
  • To get the constituent digits of a two-digit number, you only need to do `for digit in str(day)`. None of this `dayStringA, dayStringB` – smci Apr 06 '18 at 21:44
  • Then you can sum the digits with `day = sum(int(digit) for digit in str(day))`. And just wrap that in a loop `while day >= 10:` to repeatedly do the sum until you get a single digit. It's a two-liner. – smci Apr 06 '18 at 21:45

5 Answers5

2

dayStringSum is an integer, so dayStringSum[n] makes no sense. You'll want to turn it into a string first, and then look at its individual characters.

Also, you do not assign a new value to dayStringSum inside the while loop, so if it is >= 10 upon entering the loop, it will remain so, resulting in an infinite loop. You say that you got a final result of 4, but I fail to see how you would get a final result at all.

Try something like this:

daySum = int(day)  # Ensure that day is an int before we start.

while(daySum >= 10):
    newString = str(daySum)
    dayIntA = int(newString[0])
    dayIntB = int(newString[1])
    daySum = dayIntA + dayIntB  # Will be checked on next iteration.

print(daySum)
skunkfrukt
  • 1,550
  • 1
  • 13
  • 22
  • for the second final line, do you mean dayIntA instead of dayStringA, because if not, then I fail to see how the 3 statements above it have any value? I am very new to programming and trying my best. Please forgive me if it seemes like ignorance on my part. – SeesSound Oct 04 '12 at 06:17
  • Yep, that's exactly what I mean. Good catch! :-) I've edited it now. – skunkfrukt Oct 04 '12 at 06:18
  • next question refers to the statements within the "while" So the first line creates a new variable which makes the value of daysum into a string. The second and third lines split the string and convert it into integers, the last line adds the integers and gives a new value for daysum. right? If so, then what I don't understand is, if after the number is reduced the reduced number is again a double digit say 12, then why would the program look at the new value of daysum (given through last line of "while". instead of just going back to the statement above the "while" statement(in my question)? – SeesSound Oct 04 '12 at 06:30
  • yes yes yes! I finally got it, thank you so much. I've made too many silly mistakes that screw me over. Damn tunnel vision. This post was the most helpful to me. Thank you so very much! – SeesSound Oct 04 '12 at 06:43
  • @user1716168 It's recommended to accept the answer that best addresses your problem, if there is one. (Click the green checkmark) – David Z Oct 04 '12 at 07:04
1

I'm guessing the reason you're getting the wrong answer is that you add dayStringA + dayStringA when you meant to add dayStringA + dayStringB, i.e. it's just a typo.

The other thing you need to fix is that in the loop, you don't change dayStringSum. This hasn't been a problem so far because dayStringSum is less than 10 in your example, so the loop never executes in the first place, but once you fix that typo, you're going to get an infinite loop and the program will never stop.

Here's what I mean: suppose your day is 29. When you get to this line:

while(dayStringSum >=10):

then dayStringSum will be 11. So then you set dayStringA to 1,

    dayStringA= int(str(dayStringSum[0]))

and also dayStringB to 1.

    dayStringB= int(str(dayStringSum[1]))

Then that's the end of the loop. So Python goes back to this line:

while(dayStringSum >=10):

What's dayStringSum? Why, it's still 11! You never changed it. So Python will keep looping, going through the same logic over and over again.

Now beyond that, there are a bunch of things that make this code way more complicated than it needs to be. I'm not going to go through them (Code Review would be the place for that), but in general, you don't need to convert things to ints if they are already ints, and likewise you don't need to use str on something that is already a string.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • wow I have just understood loops so much better than the textbook or the professor explained it. Thank you very much. Let me get to a high enough level and im coming back to give you a +1 – SeesSound Oct 04 '12 at 06:25
0

try sum(map(int,"265"))

that maps them to ints and sums them ...

>>> sum(map(int,"254"))
11

or

>>> sum(map(int,str(29)))
11

oh well since its homework I cant really just give away the answer ....

but

its similar to

sum1=0
for integer in [1,2,3]: sum1 += integer
print sum1
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • I cant use anything more than really what I have above. We are studying beginner python and I cant use sum or map. I need to integrate looping. – SeesSound Oct 04 '12 at 06:00
  • we cannot utilize for statements either. Neither do I understand the codes map, for, and in, above. I am not asking for the answer but I believe there is some format error that I just can't notice. I've been changing the above constantly for the last 4 hrs trying to figure it out to no avail. – SeesSound Oct 04 '12 at 06:09
  • `map(int,"265")` is less preferred than a list comprehension or generator expression `[int(digit) for digit in "265"]`. But anyway we can fold all that into a generator expression: `sum(int(digit) for digit in str(day))` – smci Apr 06 '18 at 21:43
  • why is it less preferrable? and to who? – Joran Beasley Apr 06 '18 at 22:11
0

Easier way is to take modulus 9 of the number

>>> print(29%9)
2
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0
day = 29

while day >= 10:
    day = sum(int(digit) for digit in str(day))

(Also, whenever you're doing major manipulations of the individual digits of an integer, decimal.Decimal is useful, in particular its method Decimal(29).as_tuple().digits which gives you (2, 9)).

smci
  • 32,567
  • 20
  • 113
  • 146