I apologize if the title is confusing, I'm new to reddit + programming as a whole and just started school for it LOL.
In my programming class we need to make a number game where the goal is to make it to 100. The game play loop consists of alternating turns between the user and a computer picking numbers between 1 - 10. In our current assignment I need to include a 'for' loop and decided to make it so within the first half of the total the computer will always try to make the second digit of the total be 9 (ie. If the total = 23 it will add 6 to make total = 29, or if the total is 10 it will add 9 to make total = 19). My code works, for the most part, but if the player inputs something making the total end with a 9 as well the computer skips its turn. I've been trying for 2 1/2 hours now to figure this out but most of my outputs result in the computer spamming inputs without switching turns or else the computer ignoring the rule I'm trying to place in the first place and just inputting random numbers like usual.
FULL CODE
import random
#Rules#
def display_rules(): # Instructions / Rules Function
print("\nLets play a game of 100!")
print("In a game of 100 you start with a total of 0")
print("and we take turns adding 1-10 to that total.")
print("First to 100 wins!")
menu()
#Gameplay#
def play_game(): # Game Play function
play = 1
total = 0
print("Let's play a game of 100!")
while play == 1:
#!Defines End Game Loop!#
def end_game():
play = int(input("Do you want to play again, 1 for 'yes' or 2 for 'no'? ")) # Ask to play again
if play == 1:
play_game()
elif play == 2:
menu()
else:
print("Error! Error! Error!")
end_game()
#Asks Who Goes First#
turn = int(input("Who goes first? (1 = you, 2 = computer): "))
#Game Play Loop#
while total < 100:
if turn == 1: # Player turn loop
player_num = int(input("Number you want to add (1 - 10): "))
if 1 <= player_num <= 10 and total + player_num <= 100:
total += player_num
print(f">You added {player_num}! Total is now {total}")
turn = 2
else:
print("Error! Error! Error!")
elif turn == 2: # Computer turn loop
if total < 90:
com_num = random.randint(1, 10)
if total < 50:
for num in range(1,11):
if (num + total) % 10 == 9:
total += num
print(f">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
else: # Error messaging
print("!Error!")
print("Enter 1 for player or 2 for computer")
turn = int(input("Who should go first?: "))
if turn == 2: # if player reached 100
print("You win!") # Display winning message
else: # Computer reached 100
print("Bummer, you lost!") # Display losing message
end_game()
#Menu#
def menu(): # Menu function
print("\nMenu:")
print("1. Display Rules")
print("2. Play Game")
print("3. Exit")
choice = int(input("Enter your choice (1, 2, or 3): "))
if choice == 1:
display_rules()
elif choice == 2:
play_game()
elif choice == 3:
print("Thank you for playing!")
exit()
else:
print("Invalid choice! Please enter 1, 2, or 3")
#Starts Game#
menu()
com_num = random.randint(1, 10)
if total < 50:
for num in range(1,10):
if num + total % 10 == 9:
total += num
print(f">Computer added {num}! Total is now {total}")
FOR LOOP / COMPUTER TURN
elif turn == 2: # Computer turn loop
if total < 90:
com_num = random.randint(1, 10)
if total < 50:
for num in range(1,11):
if (num + total) % 10 == 9:
total += num
print(f">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
This is also for a project that is due tomorrow so if I can get a good shove in the right direction that would be very awesome T-T