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

如果我运行此代码,Python和闲置状态都将退出。我使用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()

参考方案

在函数范围之外声明您的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
>>>

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…