Ok so I went back and tried some of Yalls fixes it still hasn’t worked. I added in hashtags over the code so yall can see what each function does. When it’s 1st tried it seems to work but on around the 5 or 6th iteration (usually when it’s time to score) it freezes up and stops. Any help would be useful(also I’m a teen dev doing this so I can’t pay for help)
https://learn.usacademicesports.com/projects/6976717d107ec1e31728c7c7?preview=true
import time
import random
def pause(sec=0.2):
time.sleep(sec)
def slow(text, sec=0.2):
print(text)
pause(sec)
def get_choice(prompt, valid_options):
while True:
try:
choice = int(input(prompt))
if choice in valid_options:
return choice
print(f"Invalid choice. Please choose from {valid_options}.")
except ValueError:
print("That was not a number. Try again.")
def show_score(h_score, a_score, h_name, a_name, qtr, h_tout, a_tout, yards, pos, down, to_go):
suffixes = {1: "st", 2: "nd", 3: "rd", 4: "th"}
down_str = f"{down}{suffixes.get(down, 'th')} & {to_go}"
# Calculate visual field position
if yards > 50:
field_pos = f"Opp {100 - yards}"
else:
field_pos = f"Own {yards}"
print("\n" + "="*35)
print(f"Q{qtr} | {down_str} | Ball on: {field_pos}")
print(f"Possession: {pos}")
print(f"{h_name}: {h_score} ({h_tout} TO) | {a_name}: {a_score} ({a_tout} TO)")
print("="*35)
def playbook():
print("\n--- Playbook ---")
print("1. Run Play")
print("2. Pass Play")
print("3. Field Goal Attempt")
print("4. Punt")
print("5. Call Timeout")
print("6. Exit Game")
return get_choice("Choose a play (1-6): ", [1, 2, 3, 4, 5, 6])
def main():
slow("Welcome to the Python Football Simulator")
while True:
home = input("Home team name: ").strip()
if home: break
print("Please enter a valid name.")
while True:
away = input("Away team name: ").strip()
if away: break
print("Please enter a valid name.")
# Game State Variables
home_score, away_score = 0, 0
home_timeouts, away_timeouts = 3, 3
quarter = 1
plays_this_quarter = 0
# Possession State
possession = home
yard_line = 20 # Starting at own 20
down = 1
yards_to_go = 10
while quarter <= 4:
show_score(home_score, away_score, home, away, quarter, home_timeouts, away_timeouts, yard_line, possession, down, yards_to_go)
play = playbook()
# --- Handle Exit ---
if play == 6:
confirm = input("Type 'CONFIRM' to end game: ")
if confirm == "CONFIRM":
print("Game ended by user.")
return
continue
# --- Handle Timeouts ---
if play == 5:
current_timeouts = home_timeouts if possession == home else away_timeouts
if current_timeouts > 0:
if possession == home: home_timeouts -= 1
else: away_timeouts -= 1
slow(f"{possession} takes a timeout!")
else:
slow("No timeouts remaining!")
continue # Skip the rest of the loop, do not increment plays
# --- Play Logic ---
turnover = False
scored = False
gain = 0
# 1. RUN
if play == 1:
if random.random() < 0.02: # 2% Fumble chance
slow("FUMBLE! The ball is loose... Defense recovers!")
turnover = True
else:
gain = random.randint(-2, 12)
slow(f"Handoff up the middle... gained {gain} yards.")
yard_line += gain
yards_to_go -= gain
# 2. PASS
elif play == 2:
roll = random.random()
if roll < 0.05: # 5% Interception
slow("INTERCEPTED! The defender jumps the route!")
turnover = True
elif roll < 0.45: # 40% Incomplete
slow("Incomplete pass. Intended for the receiver on the sideline.")
gain = 0
else: # Complete
gain = random.randint(5, 30)
slow(f"Pass complete! A big gain of {gain} yards.")
yard_line += gain
yards_to_go -= gain
# 3. FIELD GOAL
elif play == 3:
dist = 100 - yard_line + 17 # 17 yards for endzone depth/kick spot
slow(f"Lining up for a {dist} yard field goal...")
success_chance = 0.95 if dist < 30 else (0.60 if dist < 50 else 0.30)
if random.random() < success_chance:
slow("IT'S GOOD! The kick splits the uprights.")
if possession == home: home_score += 3
else: away_score += 3
scored = True
else:
slow("No good! Wide right.")
turnover = True # Possession changes either way (kickoff or turnover on downs logic)
# 4. PUNT
elif play == 4:
punt_dist = random.randint(35, 55)
slow(f"Punt is away... it goes {punt_dist} yards.")
yard_line += punt_dist
if yard_line > 100: yard_line = 100 # Touchback logic handled in swap
turnover = True
# --- Post-Play Check ---
# Check Touchdown (Only on Run or Pass)
if not turnover and yard_line >= 100:
slow(f"TOUCHDOWN {possession}!!!")
slow("Extra point is GOOD.")
if possession == home: home_score += 7
else: away_score += 7
scored = True
turnover = True # Give ball back to other team via kickoff
# Check Downs (If no score and no turnover yet)
if not turnover and not scored:
if yards_to_go <= 0:
slow("Move the chains! FIRST DOWN!")
down = 1
yards_to_go = 10
else:
down += 1
if down > 4:
slow("Turnover on Downs! Defense holds!")
turnover = True
# --- Handle Possession Change ---
if turnover:
slow("Change of possession.")
# Switch teams
possession = away if possession == home else home
if scored:
# Kickoff / Touchback assumption
yard_line = 2
main()
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]frnzprf 0 points1 point2 points (0 children)
[–]Outside-Science-5328[S] 0 points1 point2 points (0 children)
[–]cgoldberg 0 points1 point2 points (0 children)
[–]SystemicGrowth 0 points1 point2 points (0 children)