2014-03-25 32 views
1

我面臨着一個關於全局變量的問題,儘管它是python聲明並不違抗。實質上,我只想檢查整數是否包含小數位,或者輸入中是否包含任何與整數無關的整數。這裏是我的代碼:全局名稱未定義?同性戀夥伴們:

def Strength1(): 
    try: 
     global strength1 
     strength1 = int(input("%s, please enter your desired strength - between 1 and 20\n>"%name1)) 
     strength1int = int(strength1) 
     def invLoop(): 
      clearScreen() 
      Invalid() 
      Strength1() 
     if int(strength1) <= 0: 
      invLoop() 
     if int(strength1) >= 21: 
      invLoop() 
    except Exception as exception: 
     clearScreen() 
     print("'%s isn't an integer."%strength1) 
     Strength1() 
def Skill1(): 
    try: 
     global skill1 
     skill1 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name1)) 
     skill1int = int(skill1) 
     def invLoop(): 
      clearScreen() 
      Invalid() 
      Skill1() 
     if int(skill1) <= 0: 
      invLoop() 
     if int(skill1) >= 21: 
      invLoop() 
    except Exception as exception: 
     clearScreen() 
     print("'%s isn't an integer."%skill1) 
     Skill1() 
def Strength2(): 
    try: 
     global strength2 
     strength2 = int(input("%s, please enter your desired strength - between 1 and 20\n>"%name2)) 
     def invLoop(): 
      clearScreen() 
      Invalid() 
      Strength2() 
     if int(strength2) <= 0: 
      invLoop() 
     if int(strength2) >= 21: 
      invLoop() 
    except Exception as exception: 
     clearScreen() 
     print("'%s' isn't an integer."%strength2) 
     Strength2() 
def Skill2(): 
    try: 
     global skill2 
     skill2 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name2)) 
     def invLoop(): 
      clearScreen() 
      Invalid() 
      Skill2() 
     if int(skill2) <= 0: 
       invLoop() 
     if int(skill2) >= 21: 
       invLoop() 
    except Exception as exception: 
     clearScreen() 
     print("'%s' isn't an integer."%skill2) 
     Skill2() 

,這是我的錯誤:

Traceback (most recent call last): 
    File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 29, in Skill1 
    skill1 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name1)) 
ValueError: invalid literal for int() with base 10: '0.5' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 197, in <module> 
    mainloop() 
    File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 188, in mainloop 
    Skill1() 
    File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 41, in Skill1 
    print("'%s isn't an integer."%skill1) 
NameError: global name 'skill1' is not defined 

回答

1

skill1沒有定義,因爲如果int(input())失敗,並拋出一個異常,那麼什麼都將被分配到skill1

首先將字符串分配給一個變量,然後嘗試轉換。

try: 
    global skill1 
    skill1 = input("%s, please enter your desired skill - between 1 and 20\n>"%name1) 
    #if the below line crashes, `skill1` will still have the old string value 
    skill1 = int(skill1) 
0

你似乎期待的是,如果用戶輸入不是一個有效的整數,skill1將被定義爲...東西。也許是用戶輸入的原始文本?無論如何,那不會發生什麼。

如果int(input(...))引發異常,則控制轉到except塊,而不分配skill1。如果即使輸入無效也要打印某些內容,則需要將結果分別保存爲input,而不是將其轉換爲整數。

0

global關鍵詞並不意味着「在全局範圍內定義變量」,而是「當搜索變量進行修改時,查看全局範圍,而不是局部範圍」。

您的代碼無法運行,您沒有呼叫定義的功能。

定義變量skill1,skill2,strength1,strength2在模塊中,而不是在功能上,它將解決您遇到的問題。

PS。全局變量很難看。不要這樣做。

1

如果您int(input('...'))調用失敗,一個Exception提高事情之前分配給skill1。然後,嘗試在錯誤處理中打印skill1的值,但該名稱尚未定義。

無論是從異常處理去除skill1(和strength1,和skill2strength2),或者給它們一些默認值(None慣例,或有時-1)的try塊外可以肯定,他們是即使用戶輸入了錯誤的值也是如此。


這有什麼做全局變量;實際上目前尚不清楚在您的代碼中是否有任何理由宣佈skill1strength1global。只需要清楚,你應該檢查docs上的global keyword:它不是定義一個名字;它所做的只是向解析器指出,在本地作用域中對該名稱的任何賦值應該應用於模塊級作用域中的該名稱。

0

的全局變量不會工作

它不會工作的Cuz如果U'r「嘗試:除了:」失敗並且不會有任何Skill1 爲什麼不ü定義變量u'r函數外部的?除非你有其他同名的人。我不推薦使用全局變量