0

i am trying the following code:

import simplegui
import random
import math
def new_game():
   global secret_number
   global low
   global high
   global n
   print "New game. Range is from" ,low ,"-",high
   secret_number=random.randrange(low,high)
   n=math.ceil(math.log((high-low+1),2))
   print "no of guesses",n
   print " "

def new_game1():
   global secret_number
   print "New game. Range is from 0-100"
   print " "
   secret_number=random.randrange(0,100)    


# define event handlers for control panel
def range100():
    global low,high
    low=0
    high=100
    new_game()

def range1000():
    global low
    global high
    low=0
    high=1000
    new_game()


def input_guess(guess):
    global secret_number
    global n
    g=int(guess)
    print "Guess was",g
    --n
    print "no of guesses left",n
    if(g>secret_number):
        print "Lower"
    elif(g<secret_number):
        print "Higher"
    else:
        print "Equal"


frame = simplegui.create_frame('Testing', 200, 200)
button1 = frame.add_button('Range is(0,100)', range100,200)
button2 = frame.add_button('Range is(0,1000)', range1000,200)
inp = frame.add_input('Enter a guess', input_guess,200)
frame.start()
new_game1() 

the problem with my above code is that I want to use a single newgame() fn which takes the default value of low as 0 and high as 100 . Right now I have bifurcated that function into newgame1() which is doing computations on the default values

How to rectify this problem? Please help

2
  • Is there any reason why all your variables need to be global? You could possibly make them local and use default arguments to save yourself some trouble. Commented Oct 2, 2014 at 21:16
  • how can i save in low=0 and high=100 for newgame() Commented Oct 2, 2014 at 21:27

1 Answer 1

1

Instead of having the limits as global variables you could send them as function arguments. You can set the default values to something and override them as needed. new_game() might become something like:

def new_game( low = 0, high = 100 ):
    global secret_number, n    # Maybe these can be function arguments as well?
    print "New game. Range is from" ,low ,"-",high
    secret_number=random.randrange(low,high)
    n=math.ceil(math.log((high-low+1),2))
    print "no of guesses",n
    print " "

Your range functions would become:

def range100():
    new_game() # high & low take default values of 0,100

def range1000():
    new_game(high = 1000) # high is now 1000

# My own function
def rangeMinus1000():
    new_game(low = -100, high = 1000) # low is -100 & high is 1000
Sign up to request clarification or add additional context in comments.

2 Comments

actually the question wants new_game to be a function with no arguments
That is unfortunately impossible. You might want to call a function like resetRange() after new_game() to reset to default values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.