Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8745575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:03:02+00:00 2026-06-13T12:03:02+00:00

I have a project that I am trying to redo. It is for a

  • 0

I have a project that I am trying to redo. It is for a coursera course on Interactive Python programming. The deadline for submission has already passed so I am not violating the honor code here, just wanting to understand why the code I am now working on for the same project is not working.

The game is Guess The Number. None of the print statements in the if block print out at all, and I of course think they should be, ergo I have made a mistake and I cannot for the life of me figure out what it is. I have gone over several other students projects during the evaluation phase and still cannot see why my code is incorrect.

The game executes in the browser here Game In Browser here

Here is the code that I am working with:

# Sanderson, Steven
# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console

import simplegui
import random
import math

# initialize global variables used in your code
secret = 0         # The secret number
guesses = 0        # The amount of guesses you will have left
guess_count = 0    # How many guesses you made
range_limit = 100  # The number range of the game


def init():
    # Here I reference the global variables because they
    # are now inside of a function being referenced locally
    global range_limit
    global secret

    # This following line limits the amount of guesses
    # the player can have based upon the given 
    # range_limit of the game
    guesses = int(math.ceil(math.log(range_limit,2)))

    # This following line of code generates a secret
    # number for the specified range of the game,
    # it has been initialized to a range of 100 to 
    # start the game
    secret = random.randrange(0, range_limit)

    # Converts range_limit to a string to print to console
    print "Play Guess The Number. The range is 0 to", + range_limit
    print "You have " +str(guesses) + " guesses for this game"
    print ""

# define event handlers for control panel

def range100():
    # button that changes range to range [0,100) and restarts
    global range_limit
    global secret
    global guesses
    range_limit = 100
    init()

def range1000():
    # button that changes range to range [0,1000) and restarts
    global range_limit, secret, guesses
    range_limit = 1000
    init()

def input_guess(guess):
    # main game logic goes here
    int_guess = int(guess)
    global guess_count
    global guesses
    global secret
    guesses += -1
    guess_count = guess_count + 1

    if int_guess == secret:
        print "You Win and did it in " +str(guess_count) + " guesses"
        print ""
        init()
    elif int_guess > secret:
        print "You guessed " +str(guess) + " guess lower"
        print "You have " +str(guesses) + " guesses left"
        print ""
    elif int_guess < secret:
        print "You guessed " +str(guess) + " guess higher"
        print "You have " +str(guesses) + " guesses left"
        print ""
    if guesses == 0:
        print "You ran out of guesses, the secret was " +str(secret)
        print ""
        init()


# create frame
frame = simplegui.create_frame("Guess The Number!", 200, 200)

# register event handlers for control elements
frame.add_button("Range [0, 100)", range100, 125)
frame.add_button("Range [0, 1000)", range1000, 125)
frame.add_input("Enter your guess here and press enter", input_guess, 125)

# start frame
frame.start()
init()

# I know my code is not the best, constructive criticisim in order to help me
# be a better coder is appreciated :)

Here is a sample output that I just ran. I printed out secret, guess_count and guesses and in that order.

71
0
0
You guessed 25 guess higher
You have -1 guesses left

71
1
-1
You guessed 25 guess higher
You have -2 guesses left

71
2
-2
You guessed 25 guess higher
You have -3 guesses left

It also looks like the init() is not working when the buttons are pressed as the count is not being reset, scratching head.init() possibly not working right either

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:03:04+00:00Added an answer on June 13, 2026 at 12:03 pm

    If you add the lines

    print repr(guess), type(guess), repr(secret), type(secret)
    print guess == secret, guess > secret, guess < secret
    

    right before the if branches, you see (something like):

    '23' <class 'str'> 4 <class 'int'>
    False False False
    

    You’re comparing a string to an integer, which in Python 3 gives TypeError: unorderable types: str() < int() but in Python 2 will silently return unexpected behaviour (based on the name of the class, if memory serves, but since we shouldn’t be doing it anyway I never manage to remember the details.) To be specific:

    >>> "47" < 200
    False
    >>> "47" < 47
    False
    >>> "47" < 2
    False
    >>> 100 < "23"
    True
    >>> 100 < "1"
    True
    >>> 100 < "-23"
    True
    

    Convert the user input to an integer, and then you can start working through the next issue :^)

    You guessed 5
    5 <class 'int'> 35 <class 'int'>
    False False True
    You guessed 5 guess higher
    You have -1 left
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that has alot of file manipulation. I am trying out
I have a project that I am trying to convert to OSGi. However, this
I have a Website project that I'm trying to debug using Fiddler. Fiddler doesn't
I have a Java project that I'm trying to implement with a model-view-controller design.
I have a working project that Im amending, it crashes after trying to use
I have an MSVC project that uses freetype, and now I'm trying to move
We have a project for an Outlook plugin .vsto that we're trying to sign
I have a Django1.1 project that works with a legacy MySQL db. I'm trying
I have a lein project (using cascalog--but that's not particularly important). I'm trying to
I have a .NETCF project that I have been trying to convert from .NETCF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.