2014-09-25 157 views
2

如何重新编写此程序以避免使用我的全局num1和num2?我有一个额外的信贷分配在我的编程类重写了几个程序,而无需使用全局变量,但是这其中有我难倒...避免在python中使用全局变量

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

把随机调用在主函数中,忘了随机函数 – 2014-09-25 23:17:26

回答

3

如果你让你randomNumbers()函数返回这样,你能避免全局状态。

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

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

如果要查找更多信息或文档,将两个值组合成一个值就称为“元组”。

+1

我猜你获得额外的信用为这项任务;-) – 2014-09-25 23:20:05

+0

@recursive据我所知,部分...但我想不通了解如何打印返回值并检查它们 – 2014-09-25 23:20:19

+0

@StevenPink:您打印并检查它们的方式与您现在的方式完全相同。根本没有改变。 – recursive 2014-09-25 23:21:00

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 

试试吗?

+0

之前,我还没有使用过多个变量的函数,这可行,但我们应该尽可能使用函数进行计算。 – 2014-09-25 23:26:18

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