Python: Guess The Number

This week’s mini-project in my Coursera Python class was to create a Guess The Number game, where the program selects a random number in a specified range (either 0-100 or 0-1000 at the player’s discretion), and the player guesses, with the program saying Higher or Lower until the player guesses correctly, or runs out of guesses.

It was a fairly basic program. We used GUI elements for the first time. Two quibbles/annoyances: Event handlers, which to me seem the same as functions. Why call them something different? It’s just a function called by an event, isn’t it? Maybe I’m missing something, and the rest of the course will clarify and show me how event handlers are distinct from functions.

The second quibble is with the course: For GUI elements, they use SimpleGUI, which is a module created especially for the class, and doesn’t exist otherwise. I would have preferred a module we could use outside the class, like tkinter. I don’t know anything about them, and would like to apply what I learn elsewhere. It seems like I’ll have to learn tkinter or something like it outside the class.

I’ll post a link to my game once the submission period for grading is over. If you are taking the class and have a question, let me know via the comments and I’ll do my best to help.

Advertisement

12 thoughts on “Python: Guess The Number

    • You’ll need to define your variables, to store the limit of the game (100 or 1000, I called mine limit), the random number the program will choose (I called it secret_number), and the maximum number of guesses (max_guesses). In your main event handler/function you will need to take the input from the user, and compare it to the secret number to see if they guess right, or if the secret number is higher or lower, then print the result. It will also need to reduce the number of guesses by one until it hits zero, ending the game and starting a new one.

      Two other event handlers (the buttons) will set the range at either 100 or 1000, by updating the limit variable and resetting the others to start new. I hope this helps. Let me know what area you are having trouble with.

  1. hi there! i’am new to programming in general not ony in python and i can’t make the function get_input (guess ) to work. I think i have all the code but it don’t print anything in the console.

  2. me again! i can run the programm now but i have a problem i don’t know what i have to put in the () in get_input here : def range1000():
    get_input()
    init()
    I know that the parameter is guess but i don’t know hte value unless the user enter it.

    • Hi Lucia – You need to register the event handler to pass the input from the user into your guess parameter. If you defined your event handler as get_input(guess), then you’d register it with something like inp = frame.add_input(“Enter guess.”, get_input, 100)

      I hope this helps.

      • thanks! when i was evaluating classmates i understand what i did wrong. I don’t have background in programming (i’m a biological scientist) and the course is starting to be a bit difficult to follow.

  3. Thank you so much. I am a student of 2013 online course and I would like to know how to use the input (guess_number) for multiple times to compare with the secret_number?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s