gpt4 book ai didi

python - 运行脚本时 Python(和空闲)意外崩溃

转载 作者:行者123 更新时间:2023-12-02 05:31:40 26 4
gpt4 key购买 nike

如果我运行此代码,Python 和idle 都会退出。我使用Python 3.2,无奈之下修改代码后可能会出现一些错误。

我的目标是创建一个文件,将代码复制到单独的文件中以供以后使用。就在最近,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()

最佳答案

在函数范围之外声明您的全局变量。由于您的脚本永远不会调用其他两个方法,因此我仅提供用于 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
>>>

关于python - 运行脚本时 Python(和空闲)意外崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com