2013-05-04 54 views
0

如果我運行此代碼,Python和空閒都會退出。我使用Python 3.2,並且在改變代碼之後可能會出現一些錯誤。運行我的腳本時發生意外的Python(和空閒)崩潰

我的目標是創建一個文件,將代碼複製到一個單獨的文件中供以後使用。就在最近,Python 2.7崩潰了,現在Python 3.2正在發生這種情況。在計算機上試用此代碼,看看會發生什麼。

請給我一些提示,因爲這很煩人。

def creating(): 
    print ('You have a total of 70 points to spend on str(strength), chr(charisma), dex(dexterity), int(intelligence), con(constitution).\nIf you put in a greater amount it will restart the character creation process; and if you put a smaller amount then you will be told you can add more.\nYou may also only have a limit of 18 on one attribute or you will restart the stat creation process.') 
    global st 
    global ch 
    global de 
    global inte 
    global con 
    #It gives me an error for the stuff above if it doesn't crash 
    st = (input('str:')) 
    ch = (input('chr:')) 
    de = (input('dex:')) 
    inte = (input('int:')) 
    con = (input('con:')) 
global scores 
score = st+ch+de+inte+inte+con 
global scores 
scores = 70-score 
#If I have the globals above it crashes and if I take it away it works. 

def bunnyoperation(): 
    rew = open('C:/Python32/charactersave.py','w') 
    rew.write(('st=')+str(st)) 
    rew.write(('\nch=')+str(ch)) 
    rew.write(('\nde=')+str(de)) 
    rew.write(('\ninte=')+str(inte)) 
    rew.write(('\ncon=')+str(con)) 
    rew.close() 
def scorecor(): 
    if score == 70 and st<19 and ch<19 and de<19 and inte<19 and con<19: 
     bunnyoperation() 
    elif score>70: 
     print ('you have a total of too many points.') 
     creating() 
    elif score<70: 

     print ('You still have ')+str(scores)+(' points left') 
     creating() 
    elif st or ch or de or inte or con>18: 
     print ('You set one of your stats as something over 18.\nYou will have to restart.') 
     creating() 
    else: 
     creating() 
creating() 
+0

我認爲你需要回顧一下你正在努力做,並且更加明確。你的代碼非常混亂(爲什麼所有這些全局變量?)。在我的系統上,它不會崩潰python或閒置(我得到一個'NameError',因爲st,ch等沒有定義:事實上它們不是......) – Salem 2013-05-04 19:40:07

+0

爲什麼你使用這麼多全局變量,爲什麼你無論如何,如果他們是全球性的,那麼在方法中聲明它們? – MentholBonbon 2013-05-04 20:09:02

+0

我得到這個錯誤:'NameError:全球名'st'沒有定義' – User 2013-05-04 20:21:18

回答

0

聲明您的global變量超出函數的範圍。由於你的腳本是永遠不會調用其他兩種方法,我只爲creating()提供腳本:

global st 
st = 0 
global ch 
ch= 0 
global de 
de= 0 
global inte 
inte= 0 
global con 
con= 0 

global score 
score=st+ch+de+inte+inte+con 

global scores 
scores=70-score 

def creating(): 
    print ('You have a total of 70 points to spend on str(strength), chr(charisma), dex(dexterity), int(intelligence), con(constitution).\nIf you put in a greater amount it will restart the character creation process; and if you put a smaller amount then you will be told you can add more.\nYou may also only have a limit of 18 on one attribute or you will restart the stat creation process.') 
    #It gives me a error for the stuff above if it doesn't crash 
    st=raw_input('str:') 
    ch=(raw_input('chr:')) 
    de=(raw_input('dex:')) 
    inte=(raw_input('int:')) 
    con=(raw_input('con:')) 

creating()呼叫請求輸入,例如

>>> 
You have a total of 70 points to spend on str(strength), chr(charisma), dex(dexterity), int(intelligence), con(constitution). 
If you put in a greater amount it will restart the character creation process; and if you put a smaller amount then you will be told you can add more. 
You may also only have a limit of 18 on one attribute or you will restart the stat creation process. 
str:3 
chr:2 
dex:5 
int:2 
con:3 
>>> 
+0

這有效。謝謝。 – user2350459 2013-05-08 00:02:46