In python 3. Create a game where the player should guess a random number between 1 and…
- User
In python 3. Create a game where the player should guess a random number between 1 and 100. The game should be re-playable, the number of guesses should be counted each run and a high score should be kept and updated each run.
- Assistant
Here's one example implementation:
import random # initialize high score to a large number high_score = float('inf') while True: # generate a random number between 1 and 100 answer = random.randint(1, 100) # initialize number of guesses to 0 num_guesses = 0 # loop until the player guesses correctly while True: guess = input('Guess a number between 1 and 100: ') num_guesses += 1 # make sure the input is a valid number try: guess = int(guess) except ValueError: print('Invalid input, please enter a number') continue # check if the guess is correct if guess == answer: print('Correct!') break elif guess < answer: print('Too low, try again') else: print('Too high, try again') # update high score if needed if num_guesses < high_score: high_score = num_guesses print(f'New high score: {high_score} guesses!') # ask if the player wants to play again again = input('Play again? (yes/no): ').lower() if again != 'yes': break print('Thanks for playing!')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.