MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / user_turn

Function user_turn

projects/HandCricket/main.py:93–138  ·  view source on GitHub ↗
(player_score, player_wickets, player_choice, over)

Source from the content-addressed store, hash-verified

91
92# Function for a player's turn
93def user_turn(player_score, player_wickets, player_choice, over):
94 print(f"Player's turn - {'Batting' if player_choice == '1' else 'Bowling'}")
95 balls = 0
96 while balls < 6 and player_wickets > 0:
97 if player_choice == "1":
98 # Get user input for batting
99 player_runs = int(
100 input(f"Over {over + 1}, Ball {balls + 1}: Enter your {'shot'} (1-6): ")
101 )
102 opponent_runs = random.randint(1, 6)
103 else:
104 # Get user input for bowling
105 opponent_choice = input(
106 f"Over {over + 1}, Ball {balls + 1}: Player 2, choose 1 to bat, 2 to bowl: "
107 )
108 player_runs = random.randint(1, 6)
109 opponent_runs = int(
110 input(
111 f"Over {over + 1}, Ball {balls + 1}: Enter your {'delivery'} (1-6): "
112 )
113 )
114
115 print(f"You chose {player_runs}, Opponent chose {opponent_runs}")
116
117 # Check if the player is out or scores runs
118 if player_choice == "1" and player_runs == opponent_runs:
119 print("Player is out!")
120 player_wickets -= 1
121 if player_wickets > 0:
122 print(f"Player has {player_wickets} wickets left.")
123 elif (
124 player_choice == "2"
125 and opponent_choice == "2"
126 and player_runs == opponent_runs
127 ):
128 print("Opponent is out!")
129 player_wickets -= 1
130 if player_wickets > 0:
131 print(f"Opponent has {player_wickets} wickets left.")
132 else:
133 player_score += player_runs
134 print(f"Player's score is {player_score}")
135 balls += 1
136
137 # Return the updated player score and wickets
138 return player_score, player_wickets
139
140
141# Function to display the scoreboard after each over

Callers 1

play_gameFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected