2016-07-03 51 views
0
from random import randint 

count = 0 
Validation = False 

def generateNumber(): 
    Number = randint(1, 10) 
    return Number 

def checkNumber(Guess): 
    Number = generateNumber() 
    if int(Guess) == Number and count < 1: 
     return "First time right! Genius?" 
     Validation = True 

    elif int(Guess) == Number and count == 2: 
     return "Correct. Second time, nice!" 
     Validation = True 
    elif int(Guess) == Number and count < 4: 
     return "Correct. Good job." 
     Validation = True 
    elif int(Guess) == Number and count > 3: 
     return "Correct. Took you long enough." 
     Validation = True 
    else: 
     return "Wrong! Try again " 

while Validation == False: 
    Guess = input("Please guess a number between 1 to 10: ") 
    print (checkNumber(Guess)) 
    count += 1 
else: 
    TryAgain = input("Would you like to try again? y/n \n") 

所以問題是,當用戶猜測正確的數字。驗證應該轉爲True。所以while循環將停止循環。但是當玩家猜測到正確的數字時,變量驗證不會變爲真。更改函數中的變量

+0

驗證被認爲是本地的,除非您在全局前綴。如果你希望從一個函數中引用你的全局驗證,你需要聲明如下:函數中的'global Validation',這種方式當你改變它的值時它會持續 –

+0

嗯。當我爲它添加全局變量時,我會在函數本身,頂部的變量聲明或while循環中執行它。它給我一個不好的語法錯誤。 – Soundwave

+0

你的代碼中的另一個主要問題是你返回一個字符串,在設置驗證字段之前有效地打破了該函數。請參閱我的答案以獲得正確的順序('Validation = True' only than'return「...」') –

回答

0

Python中的全局變量有點棘手,僅僅在文件頂部聲明一個變量是不夠的。

另外,請您設置的驗證值,然後返回的值,導致功能設置這個變量

末發行前終止是同時邏輯,見校正使用輸入和其他壓痕

的的raw_input而不是

只讀訪問全局變量按預期工作,但在函數中更改其值時需要一個額外的步驟,則應該將全局變量聲明爲全局變量。你的情況:

def generateNumber(): 
    Number = randint(1, 10) 
    return Number 

def checkNumber(Guess): 
    global Validation #<-- this instructs Python to use the global variable 
    Number = generateNumber() 
    if int(Guess) == Number and count < 1: 
     Validation = True 
     return "First time right! Genius?" 

    elif int(Guess) == Number and count == 2: 
     Validation = True 
     return "Correct. Second time, nice!" 

    elif int(Guess) == Number and count < 4: 
     Validation = True 
     return "Correct. Good job." 

    elif int(Guess) == Number and count > 3: 
     Validation = True 
     return "Correct. Took you long enough." 

    else: 
     return "Wrong! Try again " 

while Validation == False: 
    Guess = input("Please guess a number between 1 to 10: ") 
    print (checkNumber(Guess)) 
    count += 1 
    if (Validation): 
     if (raw_input("Would you like to try again? y/n \n") == "y"): 
      count = 0 
      Validation = False 
     else: 
      break 
0

您需要設置驗證之前,你從方法返回,就像這樣:

if int(Guess) == Number and count < 1: 
    Validation = True 
    return "First time right! Genius?" 

這樣做對所有if語句。

0

我發現兩(半)的問題在你的代碼:

  1. 正如Ishay佩萊德指出的那樣,你需要之前進行確認全球
  2. 你的「迴歸」的語句位於你更改「驗證」
  3. 的價值「而」沒有「其他」

下面是更新後的代碼:

from random import randint 

count = 0 
Validation = False 

def generateNumber(): 
    Number = randint(1, 10) 
    return Number 

def checkNumber(Guess): 
    global Validation 
    Number = generateNumber() 
    if int(Guess) == Number and count < 1: 
     Validation = True 
     return "First time right! Genius?" 

    elif int(Guess) == Number and count == 2: 
     Validation = True 
     return "Correct. Second time, nice!" 
    elif int(Guess) == Number and count < 4: 
     Validation = True 
     return "Correct. Good job." 
    elif int(Guess) == Number and count > 3: 
     Validation = True 
     return "Correct. Took you long enough." 
    else: 
     return "Wrong! Try again " 


while Validation == False: 
    Guess = input("Please guess a number between 1 to 10: ") 
    print (checkNumber(Guess)) 
    print "validation: %s" % str(Validation) 
    count += 1 

TryAgain = input("Would you like to try again? y/n \n") 
0

除了全局變量問題,您還需要製作另一組修復程序。 「返回」語句之後,所有賦值爲Validation = True。 Validation = True永遠不會執行!它將在執行之前退出該功能。在「返回」之前設置「Validation = True」。你仍然需要像Ishay所說的那樣擁有全球聲明。