工作但可能不是最佳的

时间:2015-03-03 22:51:04

标签: python optimization

所以这是我在python中的第二个程序,从我在这里和其他地方读到的,全局变量不是最好的东西,但是现在这就是我在我的课程中所以我希望你不要评论为什么全局变量是邪恶的。

现在该程序正在做它应该做的事情,我唯一担心的是它可能太长,没有什么,所以我要求你们帮我弄清楚我可以让它变得更简单。

这是:

import simplegui
import random
import math

#Globals, random guess and player's remaining guesses

num_range = 100
remaining_guesses = 7

# New game, range from 0 - 100 by default

def new_game():
    global num_range
    global remaining_guesses
    remaining_guesses = 7
    print "New game. Range is from 0 to 100"
    print "Number of remaining guesses is %i" %(remaining_guesses)
    print ""
    num_range = random.randrange(0, 101)
    return num_range

# Set game to a range of 0 - 100 
# after the according button is pressed
# Reset the remaining guesses to 7

def range100():
    global num_range
    global remaining_guesses
    remaining_guesses = 7
    print "New game. Range is from 0 to 100"
    print "Number of remaining guesses is %i" %(remaining_guesses)
    print ""
    num_range = random.randrange(0, 101)
    return num_range

# Set game to a range of 0 - 1000 
# after the according button is pressed
# Reset the remaining guesses to 10

def range1000():
    global num_range
    global remaining_guesses
    remaining_guesses = 10
    print "New game. Range is from 0 to 1000"
    print "Number of remaining guesses is %i" %(remaining_guesses)
    print ""
    num_range = random.randrange(0, 1001)
    return num_range

# Compare guess to random range
# Remove 1 to remaining guesses
# Display hint on the random range
# Restart the game if player is correct or out of guesses

def input_guess(guess):
    guess = float(guess)
    global remaining_guesses
    global num_range
    remaining_guesses -= 1
    print "Your guess was: %i" %(guess)
    print "Number of remaining guesses is %i" %(remaining_guesses)
    if (remaining_guesses == 0 and guess != num_range):
        print "The correct answer was %i" %(num_range)
        print "Let's try again!"
        print ""
        print ""
        new_game()
    elif (remaining_guesses == 0 and guess == num_range):
        print "Correct!"
        print ""
        print ""
        new_game() 
    elif guess > num_range:
        print "Try a lower number!"
    elif guess < num_range:
        print "Try a higher number!"
    else:
        print "Correct!"
        print ""
        print ""
        new_game() 
    print ""
    return guess, remaining_guesses

# Create & start frame

f = simplegui.create_frame("Guess the number", 200, 200)

f.add_button("Range is [0, 100]", range100, 200)
f.add_button("Range is [0, 1000]", range1000, 200)
f.add_input('My label', input_guess, 50)
f.start()

# Start a new game

new_game()

1 个答案:

答案 0 :(得分:-1)

def range(r=100, rg=7):
    global num_range
    global remaining_guesses
    remaining_guesses = rg
    print "New game. Range is from 0 to {}".format(r)
    print "Number of remaining guesses is %i" %(remaining_guesses)
    print ""
    num_range = random.randrange(0, r+1)
    return num_range

并分别按new_game()range100()range1000()替换range()range()range(1000,10)