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

Python:简单的程序输出有关问题

2012-02-07 
Python:简单的程序输出问题初学Python,请教一下。def myPrintMax(a,b):if ab:print a,is larger than,be

Python:简单的程序输出问题
初学Python,请教一下。

def myPrintMax(a,b):
  if a>b:
  print a,'is larger than',b
  elif a<b:
  print a,'is smaller than',b
  else:
  print a,'equals',b

print myPrintMax(2,3);

x=6;
y=4;
print myPrintMax(x,y);

print myPrintMax(5,5);


不解,输出:
2 is smaller than 3
None
6 is larger than 4
None
5 equals 5
None

“None”哪里来的?
谢谢

[解决办法]
我也正准备初学,呵呵,路过看热闹!
[解决办法]
注意到

print myPrintMax(2,3)

这个语句没有,这个语句的意思是 print(打印)myPrintMax函数的返回值,而你的myPrintMax函数没有设定返回值,所以便是自动返回None值了,如果你在定义myPrintMax函数时加多一句:return 0,例如下面的代码:

Python code
def myPrintMax(a,b):    if a>b:        print a,'is larger than ',b    elif a < b:        print a,'is smaller than', b    else:        print a ,' equlas ',b        return 0print myPrintMax(2,3) 

热点排行