How can I make the computer take its turn when no number reaches a total ending in 9?

October 4, 2026 4 answers
Asked by !simmer gently
I'm building a beginner Python game where the player and computer take turns adding a number from 1 to 10 to a running total. The first person to reach 100 wins. For part of the game, I want the computer to choose a number that makes the total end in 9โ€”for example, adding 6 to 23 to reach 29. My current computer-turn code uses a for loop to test numbers from 1 through 10. However, when the total already ends in 9, no number satisfies the condition, so the loop finishes without changing the total or switching turns. This can cause the computer to appear to skip its turn. How should I handle that case, and are there any other issues with the loop or modulo expression that I should fix?

Answers

be.right.back.
The turn-switching logic should be outside the matching loop. The computerโ€™s turn is one complete action: choose a number, add it once, print it, and then set `turn = 1`. If `turn = 1` is only reached in one branch, the computer can get stuck whenever the preferred number is not found.
Reply to this answer
narrow-band
If you keep the `for` loop for the assignment, remember that `range(1, 11)` includes 10; `range(1, 10)` stops at 9. Once a matching number is found, add it, print the result, and use `break` so the loop does not continue making multiple moves in one computer turn. A structure like this would work: ```python if total < 50: moved = False for num in range(1, 11): if 1 <= num <= 10 and total + num <= 100 and (total + num) % 10 == 9: total += num print(f"Computer added {num}! Total is now {total}") moved = True break if not moved: com_num = random.randint(1, min(10, 100 - total)) total += com_num print(f"Computer added {com_num}! Total is now {total}") turn = 1 ``` The `moved` flag handles cases where the loop finds no match, including totals that already end in 9.

satin.weave: Also be careful with the expression used in the condition. Write `(num + total) % 10 == 9`. Without those parentheses, Python evaluates `%` before `+`, which changes the test to `num + (total % 10) == 9`.

Reply to this answer
_not_today_
Your loop may be running correctlyโ€”it simply finds no valid number and then exits. That is normal `for`-loop behavior. The problem is that your program has no fallback after the loop. Use an `else` attached to the `for` loop, or a flag, to detect that no number matched and then make a random legal move. You should also stop after the first successful match. Without `break`, the loop can add more than one number if multiple values satisfy the condition, causing the computer to take several turns at once.
Reply to this answer
!512 grains of sand
You donโ€™t really need a loop to find the desired number. If the last digit of the total is `last_digit = total % 10`, then the amount needed to reach a number ending in 9 is `9 - last_digit`. For example, if the total is 23, that gives 6. However, when the total already ends in 9, the result is 0, and adding 0 is not allowed. Handle that case separately by choosing another valid move, such as 10 or a random number. Also make sure the chosen move will not take the total over 100.

Peregrination: The important part is that every computer-turn path must assign a move and set `turn = 1`. If the computer cannot follow the preferred strategy, use a fallback move instead of letting the turn end without changing anything.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions