【问题标题】:scipy.stats converter normal z scores to p value Python3scipy.stats 转换器正常 z 分数到 p 值 Python3
【发布时间】:2019-08-02 10:03:33
【问题描述】:

用这个小转换器有点挣扎,我无法让它通过第一个输入,这是反复要求的。有没有更优雅的方法来解决让我脱离循环的 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】:

    想了想代码怎么错了,忘了查基础:

    在处理之前转换您的输入!!!

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

    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
    

    【讨论】:

      猜你喜欢
      • 2011-03-30
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      相关资源
      最近更新 更多