简体   繁体   中英

Unexpected Python (and idle) crash when running my script

Python and idle both get exited if I run this code. I use Python 3.2 and there may be a few errors made after changing code out of desperation.

My goal is to create a file that copies code into a separate file for later use. Just recently, Python 2.7 crashed and now this is happening with Python 3.2. Try this code out on your computer and see what happens.

Please give me some tips, because this is very annoying.

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

Declare your global variables outside the scope of your function. Since your script is never calling the two other methods, I provide the script only for 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:'))

Call of creating() asks for inputs, and for instance

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM