避免在python中使用globals

2024-09-20 03:48:05 发布

您现在位置: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()

Tags: theanswermaindeffunctionrandomprintnumbers
3条回答

如果您让randomNumbers()函数像这样返回,您可以避免全局状态。你知道吗

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

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

如果您想查找更多信息或文档,将两个值组合成一个值的方式称为“元组”。你知道吗

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 

试试看?你知道吗

# 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()

相关问题 更多 >