首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python错误

2012-07-05 
python异常Python codewhile True :try:x raw_input(Enter the first number :)y raw_input(Enter

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() 

热点排行