与这个小小的转换器挣扎,我无法让它带我通过第一个输入,这是反复要求。有什么更优雅的方法可以让我摆脱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发布于 2019-03-13 16:09:25
在考虑了代码是如何错误的之后,我忘了检查基本的内容:
在处理前转换输入!
我只是把这两个输入都转换成了浮点数,现在它就像一个咒语一样工作,包括在输入无效的情况下请求新输入。
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=1https://stackoverflow.com/questions/55112903
复制相似问题