How do I make it so I’m able to play a full at bat
I’m making a baseball simulation game. And I have it so you are able to decide to take a pitch swing or bunt . I have it where you can choose but it only lets you choose once then it moves on here’s my code:
import random
def play_game(lineup):
batter_index = 0
your_score = 0
cpu_score = 0
input("\nPress Enter to start the game...")
for inning in range(1, 10):
print("\n===========")
print("Inning", inning)
print("===========")
outs = 0
first_base = False
second_base = False
third_base = False
runs_this_inning = 0
while outs < 3:
batter = lineup[batter_index]
print("\nBatting:", batter)
if batter == "YOU":
print("1. Take pitch")
print("2. Swing")
print("3. Bunt")
choice = input("Choose 1-3: ")
if choice == "1":
result = random.choice(["Ball", "Strike"])
print("Result:", result)
elif choice == "2":
result = random.choice(["Groundout", "Flyout", "Single", "Double", "Home Run"])
print("Result:", result)
elif choice == "3":
result = "Out"
print("Result: Bunt attempt = Out")
else:
result = "Out"
if result in ["Groundout", "Flyout", "Out"]:
outs += 1
elif result == "Single":
if third_base:
runs_this_inning += 1
third_base = second_base
second_base = first_base
first_base = True
elif result == "Double":
if third_base:
runs_this_inning += 1
if second_base:
runs_this_inning += 1
third_base = first_base
second_base = True
first_base = False
elif result == "Home Run":
runs = 1
if first_base:
runs += 1
if second_base:
runs += 1
if third_base:
runs += 1
runs_this_inning += runs
first_base = False
second_base = False
third_base = False
else:
result = random.choice([
"Strikeout",
"Groundout",
"Flyout",
"Walk",
"Single",
"Double",
"Home Run"
])
print("Result:", result)
if result in ["Strikeout", "Flyout", "Groundout"]:
outs += 1
elif result == "Single":
if third_base:
runs_this_inning += 1
third_base = second_base
second_base = first_base
first_base = True
elif result == "Double":
if third_base:
runs_this_inning += 1
if second_base:
runs_this_inning += 1
third_base = first_base
second_base = True
first_base = False
elif result == "Home Run":
runs = 1
if first_base:
runs += 1
if second_base:
runs += 1
if third_base:
runs += 1
runs_this_inning += runs
first_base = False
second_base = False
third_base = False
elif result == "Walk":
if not first_base:
first_base = True
elif not second_base:
second_base = True
elif not third_base:
third_base = True
batter_index += 1
if batter_index == len(lineup):
batter_index = 0
print("Outs:", outs)
your_score += runs_this_inning
print("\n--- End Inning ---")
print("You:", your_score)
print("CPU:", cpu_score)
print("Runs this inning:", runs_this_inning)
input("\nPress Enter to continue...")