cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1662
Views
0
Helpful
3
Replies

2.1.3.8 Lab - create a simple game with python IDLE

Khadkay
Level 1
Level 1

Hello,

Having written the code as I saw it and corrected evident errors while trying to run the code in python IDLE, i do not know why i am unable to type/insert a number do the program can continue running.

Task is to create a game where the program guesses the number the player chooses.

assistance please.vb.png

3 Replies 3

It seems like you're working on a number guessing game where the program guesses the number chosen by the player. To help you further, I'd need to see the code you've written so far. However, I can provide you with a simple outline of how you can implement such a game in Python:

  1. Import the random module to generate random numbers.
  2. Define the minimum (min_number) and maximum (max_number) values for the range of numbers the player can guess.
  3. Generate a random target number within the specified range using random.randint.
  4. Initialize variables for tracking the number of guesses (guesses) and whether the guess is correct (is_guess_correct).
  5. Print a welcome message and instructions to the player.
  6. Enter a while loop that continues until the player's guess is correct.
  7. Inside the loop:
    • Attempt to get the player's guess as an integer using input and int.
    • Increment the number of guesses.
    • Check if the guess is correct, too low, or too high and provide appropriate feedback to the player.
    • Handle invalid input with a try and except block, informing the player to enter a valid number.
  8. Once the player guesses the correct number, exit the loop and print a congratulatory message with the target number and the number of guesses it took.

You can take this breakdown and write the code in your Python environment. If you encounter any specific issues while implementing it, feel free to ask for help with those particular problems.

This code sets up a simple number guessing game where the player needs to guess the randomly generated target number within a specified range. It uses a while loop to continue taking guesses until the player guesses correctly.

Ruben Cocheno
Spotlight
Spotlight

@Khadkay 

Has this been resolved?

Tag me to follow up.
Please mark it as Helpful and/or Solution Accepted if that is the case. Thanks for making Engineering easy again.
Connect with me for more on Linkedin https://www.linkedin.com/in/rubencocheno/

alexfly1330
Level 1
Level 1

It seems like you're encountering an issue with your code related to taking input from the user. Without seeing the actual code, I can provide you with a general example of how to take user input in Python. Make sure you're using the input() function to get user input.

Here's a simple example that you can adapt for your guessing game:

 

import random

def guess_the_number():
    # Generate a random number between 1 and 100
    secret_number = random.randint(1, 100)

    while True:
        try:
            # Get user input
            user_guess = int(input("Guess the number (between 1 and 100): "))

            # Check if the guess is correct
            if user_guess == secret_number:
                print("Congratulations! You guessed the correct number.")
                break
            elif user_guess < secret_number:
                print("Too low. Try again.")
            else:
                print("Too high. Try again.")

        except ValueError:
            print("Invalid input. Please enter a valid number.")

if __name__ == "__main__":
    guess_the_number()

 

In this example, the program generates a random number between 1 and 100, and the user is prompted to guess the number. The program then provides feedback on whether the guess is too high, too low, or correct. If the user enters a non-numeric value, a ValueError exception is caught, and the user is prompted to enter a valid number.

If you're still having issues after adapting your code with this example, please share your code, and I'll do my best to assist you further.