First ever lines of code in Py. For testing go to cloud www.codesculptor.org and paste the code in canvas, then press run. Week 2, assignment 2.
# 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 num_range=100 gaming_var=50 choice=0 nr_guess=1 num_guess=7 msg_lost='' #helper func to init game def init(): #5 lines of code global num_range,gaming_var,num_guess,nr_guess,msg_lost if num_range==100: num_guess=7 else: num_guess=10 gaming_var=random.randrange(0, num_range) nr_guess=0 msg_lost='' print '' print "New game. Range is from 0 to " + str(num_range) print "Number of remaining guesses is " + str(num_guess) print num_guess, nr_guess # define event handlers for control panel """def get_input(input): global choice choice=int(input) print choice""" def range100(): #3 lines of code global num_range num_range=100 init() # button that changes range to range [0,100) and restarts def range1000(): #3 lines of code global num_range num_range=1000 init() # button that changes range to range [0,1000) and restarts def get_input(guess): #14 lines of code # main game logic goes here global num_range,gaming_var,choice,num_guess,nr_guess,msg_lost #choice=get_input choice=int(guess) nr_guess +=1 print '' print 'Guess was ' + guess print 'Number of remaining guesses is ' + str(num_guess-nr_guess) if choice>gaming_var: print 'Lower!' elif choice<gaming_var: print 'Higher!' elif choice==gaming_var: msg_lost='Correct!' print msg_lost print '' init() else: print 'ERROR OCCURED!' if nr_guess>num_guess and msg_lost!='Correct!': print "You Lost!" init() # create frame f=simplegui.create_frame('Guess the number',200,200) # register event handlers for control elements f.add_button("Range is [0..100)", range100, 200) f.add_button("Range is [0..1000)", range1000, 200) f.add_input("Enter a guess", get_input, 200) # start frame init() # always remember to check your completed program against the grading rubric
Leave a Comment