避免在python中使用全局变量

时间:2014-09-25 23:15:30

标签: python

如何重新编写此程序以避免使用全局num1和num2?我在我的编程课中有一个额外的学分,在没有使用全局变量的情况下重写几个程序,但是这个让我感到难过......

# Define the main function
def main():
    randomNumbers()
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

# Define a function to generate random numbers
def randomNumbers():
    import random # Imports the random module

    # Generates two random numbers to be added
    global num1
    global num2
    num1 = random.randrange(100,1000)
    num2 = random.randrange(100,1000)

# Call the main function
main()

弄明白了!非常感谢你!

# Define the main function
def main():
    num1, num2 = randomNumbers()
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

# Define a function to generate random numbers
def randomNumbers():
    import random # Imports the random module
    rand1 = random.randrange(100,1000)
    rand2 = random.randrange(100,1000)
    return rand1, rand2

# Call the main function
main()

3 个答案:

答案 0 :(得分:3)

如果您使randomNumbers()函数返回此类,则可以避免全局状态。

def main():
    # calculate something
    num1, num2 = randomNumbers()
    # calculate something else

def randomNumbers():
    # calculate something
    return num1, num2

将这两个值组合成一个值称为"元组"如果您想查找更多信息或文档。

答案 1 :(得分:1)

# Define the main function
def main():
    num1 = randomNumbers()
    num2 = randomNumbers()
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

# Define a function to generate random numbers
def randomNumbers():
    import random # Imports the random module

    # Generates two random numbers to be added
    number = random.randrange(100,1000)
    return number


# Call the main function
main()

答案 2 :(得分:0)

def main():
    num1 = random.randrange(100,1000)
    num2 = random.randrange(100,1000)
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

试试吗?