scipy.stats转换器正常z分数为p值Python3

问题描述 投票:1回答:1

这个小转换器有点挣扎,我无法让它超过第一个输入,这是反复要求的。有没有更优雅的方法来解决让我摆脱循环的ValueError问题?

编辑:我也玩了a = 1和a = 0的位置,当我这样做时,它停止询问我输入,但它只是运行脚本而不要求我输入第二个用户。

谢谢大家!

import scipy.stats as st
a=1
while a==1:
    try:
        choice = input('Press 1 for percentages to Z-Score, 2 for Z-score into percentages, one tailed')
        if choice ==1:
            percentage = input('Enter value')
            print(st.norm.ppf(percentage))
            a=0
        if choice ==2:
            score = input('Enter value')
            print(st.norm.cdf(score))
            a=0
    except ValueError:
        print('Invalid Entry')
        a=1
python-3.x while-loop scipy normal-distribution valueerror
1个回答
0
投票

在考虑代码是如何错误之后,我忘了检查基础知识:

在处理前转换输入!

我刚刚将两个输入都转换为浮点数,现在它就像一个魅力,包括在输入无效的情况下请求新输入。

import scipy.stats as st
a=1
while a==1:
    try:
        float(choice = input('Press 1 for percentages to Z-Score, 2 for Z-score into percentages, one tailed'))
        if choice ==1:
            percentage = float(input('Enter value'))
            print(st.norm.ppf(percentage))
            a=0
        if choice ==2:
            score = float(input('Enter value'))
            print(st.norm.cdf(score))
            a=0
    except ValueError:
        print('Invalid Entry')
        a=1
© www.soinside.com 2019 - 2024. All rights reserved.