Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

How can i add a While loop to detect errors in the input in python

So, i am trying to write a code to not only do arithmetic equations, but also, give feedback if there is an error as well as give me 3 tries max. any advice?

arithmetic=input("Enter an arithmetic operation:")
arithmetic= arithmetic.replace(" ", "")  
w=arithmetic.split('/')
x= arithmetic.split('-')  
y=arithmetic.split('+')
z=arithmetic.split('*')
if ("+" in arithmetic):
    a= int(y[0])
    b= int(y[1])
    sum = a + b
    print("The result is = " + str(sum))
elif ("*" in arithmetic):
    a= int(z[0])
    b= int(z[1])
    result = a * b
    print("The result is = " + str(result))            
elif ("/" in arithmetic):
    a= int(w[0])
    b= int(w[1])
    result = a / b
    result = round(result,3)
    print("The result is = " + str(result)) 
    
elif ("-" in arithmetic) and (len(x)==3):  
    a= int(x[1])
    b= int(x[2])
    result = a + b                    
    result = result  * (-1)
    print("The result is = " + str(result))    
    
elif ("-" in arithmetic) and (len(x)==2):  
    a= int(x[0])
    b= int(x[1])  
    result = a - b
    print("The result is = " + str(result)) 
#tries = 0
#while(tries  < 3):
#    arithmetic=input("Enter an arithmetic operation:")
#    match = arithmetic.find('+')
#    print(match)
#    if(match == -1): 
#        print ("Invalid")
#        tries += 1
#    else:
#        tries= 3

I tried to add the while in the beginning. However, when i put an input such as 11 and 12 without the + sign, it just printed the input without giving me an error. why is that?

question from:https://stackoverflow.com/questions/65930952/how-can-i-add-a-while-loop-to-detect-errors-in-the-input-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
tries = 0

while tries < 3:
    arithmetic = input("Enter an arithmetic operation:")
    arithmetic = arithmetic.replace(" ", "")
    w = arithmetic.split('/')
    x = arithmetic.split('-')
    y = arithmetic.split('+')
    z = arithmetic.split('*')
    try:
        if ("+" in arithmetic):
            a = int(y[0])
            b = int(y[1])
            sum = a + b
            print("The result is = " + str(sum))
        elif ("*" in arithmetic):
            a = int(z[0])
            b = int(z[1])
            result = a * b
            print("The result is = " + str(result))
        elif ("/" in arithmetic):
            a = int(w[0])
            b = int(w[1])
            result = a / b
            result = round(result, 3)
            print("The result is = " + str(result))

        elif ("-" in arithmetic) and (len(x) == 3):
            a = int(x[1])
            b = int(x[2])
            result = a + b
            result = result * (-1)
            print("The result is = " + str(result))
        elif ("-" in arithmetic) and (len(x) == 2):
            a = int(x[0])
            b = int(x[1])
            result = a - b
            print("The result is = " + str(result))
        else:
            print("Invalid")
            tries += 1
            continue
        break
    except ValueError:  # for case split not working good due cases like first input is a negative number
        print("Invalid")
        tries += 1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...