gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Generate a small game code that can be run directly
- User
Generate a small game code that can be run directly
- Assistant
Sure, here's a small game code for a simple guessing game in which the user needs to guess a randomly generated number:
codeimport random def guess_number(): random_number = random.randint(1, 100) attempts = 0 while True: user_guess = int(input("Guess a number between 1 and 100: ")) attempts += 1 if user_guess < random_number: print("Too low, try again!") elif user_guess > random_number: print("Too high, try again!") else: print(f"Congratulations! You guessed the number in {attempts} attempts.") break guess_number()To run this code, simply copy and paste it into a Python file (e.g.,
game.py), and run the file using a Python interpreter. The game will then start, and the user can input their guess for the randomly generated number. The program will provide feedback if the guess is too high or too low until the correct number is guessed.