1 Replies - 2387 Views - Last Post: 12 March 2010 - 11:17 AM Rate Topic: -----

#1 kyle91st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-March 10

playing "guessing game" in Graphics window

Posted 11 March 2010 - 05:41 PM

hey everyone,
I am very new to programming. I want to run the guessing game in a graphics window(as opposed to just in idle).
Im really stuck on how to get the user inputs from the graphics window to go through the while loop and then output back onto the graphics window.

This is the code i have so far. The guessing game itself works in idle, it is just a matter of getting it to work in the window.

Thanks


from random import *
from graphics import *

def main(max):

    #window
    win=GraphWin("The Guessing Game",700,700)
    win.setCoords(0.0,0.0,3.0,5.0)
    win.setBackground("ivory")


    Text(Point(1.5,4.5),"\nI'm thinking of a number between 1 and 100.").draw(win)
    Text(Point(1.5,4.2), "Try to guess the number in as few attempts as possible.No more than 10!\n").draw(win)
    Text(Point(1,3), "Your Guess").draw(win)
    Text(Point(1,1),"Answer").draw(win)
    input=Entry(Point(2,3),5)
    input.setText("0.0")
    input.draw(win)
    output=Text(Point(2,1),"")
    output.draw(win)
    button=Text(Point(1.5,2.0), "Guess")
    button.draw(win)
    Rectangle(Point(1,1.5),Point(2,2.5)).draw(win)

    win.getMouse()
   
    #game
    print("\tGuess the number!")
    print("\nI'm thinking of a number between 1 and 100.")
    print("Try to guess the number in as few attempts as possible.No more than 10!\n")
    num = randrange(101)

    
    tries = 0
    guess = ""
    while guess != num:
        guess = int(input("Take a guess: "))
        if guess > num:
            print("Too high.")
        elif guess < num:
            print ("Too low.")
        tries += 1
        if tries >= max:
            print("Sorry, you took too many guesses. Game Over")
            exit()
    print("Congratulations!")
    again =input("To play again press Enter. Type anything else to quit.")
    if again == "":
        main(max)
    else:
        exit()



    
    
main(10)


This post has been edited by kyle91st: 11 March 2010 - 05:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: playing "guessing game" in Graphics window

#2 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

Re: playing "guessing game" in Graphics window

Posted 12 March 2010 - 11:17 AM

Take a look at this website: http://www.difranco....es/graphics.htm. Install graphics.py and it should work. This should help. One thing I'd like to point out is you have num = randrange(101). It should be
num = randint(101)]
. Also you have not defined a range. Create two boxes that accept a number for a low range and high range. All you have said was guess a number between 1 and 100. You also need to create a while loop for a max of only 10 tries. Let me know if you get stuck and I can add the rest for you. Here is a sample code of what I did for a simple guessing game. Add two more boxes: one for low range and one for high range just like you did for the guess box. All your going to do is duplicate some code and place it in the right places. Hope this helps:

    low_range = input('Enter lower range of number: ')
    high_range = input('Enter upper range of number: ')
    random = randint(low_range, high_range)
    guess = input('Enter your guess: ')
    if guess < low_range or guess > high_range:
        print 'Your guess is outside the valid range', low_range, '-', high_range
    elif guess > random:
        print 'Sorry, the number was', random, ',', guess, 'is too high'
    elif guess < random:
        print 'Sorry, the number was', random, ',', guess, 'is too low'
    elif guess == random:
        print 'Congratulations! You guessed right.'


This post has been edited by j1_osu2002: 12 March 2010 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1