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

0
0
Asked By MellowBirch42 On

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?

4 Answers

Answered By QuietHarbor7 On

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.

CopperLynx19 -

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.

Answered By SunnyCobalt31 On

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.

Answered By PixelCedar5 On

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.

RustyMaple88 -

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`.

Answered By VelvetNook64 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.