Python 笔记 1
下面这个 function 是有问题的:
#!/usr/bin/python
#Filename:
def func(x):
global x;
print "x is",x
x=2;
print "Change local x to",x;
x=50;
func(x);
print 'x is now', x;
问题在于 fun(x) 中的 x 本身是个 local 的变量,而又声明为 global 的。也就是 x 既是local的也是global的。
正确的应该是:
def func():
global x;
print "x is",x
x=2;
print "Change local x to",x;
x=50;
func();
print 'x is now', x;
===========================
[root@localhost tmp]# cat ./repeat.py
#!/usr/bin/python
#Filename:
def say(message,times=1):
print message*times;
str=raw_input("Input ONE word pls:");
time=int(raw_input("Input a number pls:"));
say(str,time);