无效输入后重新插入

时间:2011-07-11 16:55:39

标签: python loops input while-loop

在我尝试输入进行有效性检查之前没有问题,如果无效则再次询问输入 我提前感谢你的想法:)

a=0
def reinp(a,b):
    while True:
        if a in [1,2,3,4,5,6]: #checking for valid input
            return int(a)
            break
        a=input(b)
    else:
        return print("error")

试过,不工作

def reinp(a,b):
    for c in [1,2,3,4,5,6]:
        if int(c)==int(a):
            return int(a)
            break
        else:
            a=input(b)

a=reinp(a,'Test: ')

这是第一个提出问题的人

def reinp2(a,b): #trying to check if it's a number and can be turned to float if not ask again
    while check(a):
        a=input(b)
    return float(a)
def check(a):
    try:
        float(a)
        return False
    except ValueError:
        return True

现在问题出在检查之后,它永远不会从任何while循环中解脱出来 我试着取代while True:if...breakwhile correct: if... correct=False 没用 它只是一次又一次地问,即使满足条件...... python 3.2中没有raw_input,所以我也不能使用它 reinp2()是这样的,如果找到reinp()的解决方案同样适用于reinp2(),则a和b只是变量ans[n]=reinp2(ans[n],"Input n: ")reinp()相同仅适用于另一种类型的变量(也可以是浮点数)

现在的代码显示没有语法错误

P.S。我正在使用Python 3.2

2 个答案:

答案 0 :(得分:1)

[编辑:已删除原始答案,因为不再与问题中的固定格式相关]

reinp的问题是a将是一个字符串,并且您将根据整数进行检查。
......所以改变:

    if a in [1,2,3,4,5,6]: #checking for valid input

为:

    if a in ['1','2','3','4','5','6']: #checking for valid input

如果您仍然遇到reinp2问题,也许您可​​以展示一些演示该问题的代码。它看起来很好。

答案 1 :(得分:1)

P.S。它是完整的我只是想让所有帮助知道它运行的人没有任何故障我甚至定制它以便它可以接收初始数据:)如果有人需要一个排列求解器你知道在哪里找到它:))

如果有人想要这个剧本:

from math import *
ans=['n','k','choice',0,0,0,0,0]
n,k=0,1
a=['''1 For Permutations P (from n) = n
2 For Variations V (k emelments from n-th class) = n!/(n-k)!
3 For Combinations C (k emelments from n-th class) = n!/(k!(n-k)!) = ( n )
4 Use last answer.                                                                  ( k )
5 Second Memory
6 Clear memory

Your choice is : ''',
'''+ to add
- to substract
* to multiply
/ to divide
You will undertake?: ''',
"The answer is: "]
def perm():
    global ans
    ans[n]=reinp2(ans[n],"Input n: ")
    if ans[5]==0:
        ans[3]=factorial(ans[n])
        ans[6]=ans[3]
        return print(a[2], ans[6])
    else:
        ans[4]=factorial(ans[n])
        ans[6]=ops(ans[3],ans[4],ans[5])
        return print(a[2], ans[6])
    ans[n]=''
    ans[k]=''
def var():
    global ans
    ans[n]=reinp2(ans[n],"Input n: ")
    ans[k]=reinp2(ans[k],"Input k: ")
    if ans[5]==0:
        ans[3]=factorial(ans[n])/(factorial(ans[n]-ans[k]))
        ans[6]=ans[3]
        return print(a[2], ans[6])
    else:
        ans[4]=factorial(ans[n])/(factorial(ans[n]-ans[k]))
        ans[6]=ops(ans[3],ans[4],ans[5])
        return print(a[2], ans[6])
    ans[n]=''
    ans[k]=''
def comb():
    global ans
    ans[n]=reinp2(ans[n],"Input n: ")
    ans[k]=reinp2(ans[k],"Input k: ")
    if ans[5]==0:
        ans[3]=factorial(ans[n])/((factorial(ans[n]-ans[k]))*(factorial(ans[k])))
        ans[6]=ans[3]
        return print(a[2], ans[6])
    else:
        ans[4]=factorial(ans[n])/((factorial(ans[n]-ans[k]))*(factorial(ans[k])))
        ans[6]=ops(ans[3],ans[4],ans[5])
        return print(a[2], ans[6])
    ans[n]=''
    ans[k]=''
def ent():
    global ans,a
    ans[2]=reinp(ans[2],a[0])
    if ans[2]==5:
        if ans[3]!=0:
            ans[7]=ans[3]
        print(ans[7])
        ent()
    if ans[2]==6:
        clear()
        print("Done!")
        ent()
    if ans[3]==0 and ans[2]==4:
        print('The memory is empty...')
        ent()
    elif ans[3]!=0 and ans[2]==4:
        ans[3]=ans[3]
        ans[5]=reinp1(ans[5],a[1])
        if ans[5] == '+' :
            ans[5]='add'
            print("Adding")
        elif ans[5] == '-' :
            ans[5]='sub'
            print("Substracting")
        elif ans[5] == '*' :
            ans[5]='mul'
            print("Multiplication")
        elif ans[5] == '/' :
            ans[5]='div'
            print("Dividing")
        ans[2]='choice'
        ent()
    if ans[2]==1:
        perm()
    elif ans[2]==2:
        var()
    elif ans[2]==3:
        comb()
    clear1()
    ent()
def ops(a,b,c):
    if c=='add':
        return a+b
    if c=='sub':
        return a-b
    if c=='mul':
        return a*b
    if c=='div':
        return a/b
def reinp(a,b):
    while True:
        a=input(b)
        if str(a) in ['1','2','3','4','5','6']:
            return int(a)
            break    
        else:
            print('There was an error please try again:')
def reinp1(a,b):
    while True:
        a=input(b)
        if a in ["+", "-", "*", "/"]:
            return a
            break
def reinp2(a,b):
    while check2(a):
        a=input(b)
    return float(a)
def check2(a):
    try:
        float(a)
        return False
    except ValueError:
        return True
def clear():
    ans[0]='n'
    ans[1]='k'
    ans[2]='choice'
    ans[3]=0
    ans[4]=0
    ans[5]=0
    ans[7]=ans[6]
    ans[6]=0
def clear1():
    ans[0]='n'
    ans[1]='k'
    ans[2]='choice'
ent()