I'm building a Python number game where the player and computer take turns adding 1–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 get 29, or adding 9 to 10 to get 19.
My current computer-turn code loops through possible numbers:
```python
elif turn == 2:
if total < 90:
com_num = random.randint(1, 10)
if total Computer added {num}! Total is now {total}")
else:
total += com_num
turn = 1
else:
com_num = 100 - total
total += com_num
print(f">Computer added {com_num}! Total is now {total}")
turn = 1
```
The problem is that if the total already ends in 9, the loop finds no valid number, so the computer appears to skip its turn. In other versions, the loop adds multiple numbers because it keeps going after finding a match. How should I handle the no-match case while making sure the computer only adds one number?
3 Answers
A `for` loop is not really necessary to find the desired number. If the last digit of `total` is `d`, the number needed to reach a total ending in 9 is `9 - d` (or 9 when the last digit is 0). You can calculate that directly, then choose a fallback when the result is not between 1 and 10. For example:
```python
needed = (9 - total % 10) % 10
if 1 <= needed <= 10 and total + needed Computer added {com_num}! Total is now {total}")
turn = 1
```
When the total already ends in 9, `needed` is 0, which is not a legal move, so the fallback strategy is used. Also, keeping the addition outside the loop guarantees that only one number is added.
The loop should represent a search, not the computer’s entire turn. Search through the legal choices, stop when one works, and then handle the case where the search found nothing with an `else` block or a flag. Also, the computer should not add inside every matching iteration without a `break`, because that can make its total jump several times during one turn.
There are two separate issues in the original condition. First, `range(1, 10)` checks 1 through 9, not 10; use `range(1, 11)` if 10 is allowed. Second, make sure the entire sum is grouped: `(num + total) % 10 == 9`. Without those parentheses, Python evaluates it as `num + (total % 10) == 9` because the modulo operator has higher precedence.
You also need to stop after the first successful match:
```python
found = False
for num in range(1, 11):
if total + num Computer added {num}! Total is now {total}")
found = True
break
if not found:
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 fallback handles cases such as a total ending in 9, where no number from 1 to 10 produces another total ending in 9.
The important part is that `turn = 1` should happen after the computer has either found a number or used the fallback. Otherwise it is easy for the outer loop to repeat the computer logic without a real move being made.

If the assignment specifically requires a `for` loop, you can still use one to search for the move, but add a `break` immediately after finding a match and keep a flag so you know whether the loop found anything. Otherwise every matching iteration can modify the total.