2014-09-25 154 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()