python异常
- Python code
while True : try: x = raw_input("Enter the first number : ") y = raw_input("Enter the second number : ") value = x/y print "x/y is ", value except : print "something wrong , please try again " else : break结果如下:
Enter the first number : 10
Enter the second number : 2
something wrong , please try again
Enter the first number : 10
Enter the second number : 0
something wrong , please try again
Enter the first number : 10
Enter the second number : 5
something wrong , please try again
Enter the first number :
问题:输入第一个数为10,第二个数为2 。。还是得不到正确结果5.
[解决办法]
value = int(x)/int(y)
[解决办法]
another way:
- Python code
#!python# encoding: utf-8import syserrStream = sys.stderroutStream = sys.stdoutdef getInt(): while True: try: n = int(raw_input('Enter an integer number:')) except: errStream.write('integer input error, try again\n') continue return ndef main(): while True: x = getInt() y = getInt() try: outStream.write('%d / %d is %d\n'%( x, y, x/y)) except: errStream.write('some error in div, try again\n') else: breakif __name__ == '__main__': main()