def average(*args):
sum=0.0
if len(args)==0:
return sum
else:
for x in args:
sum+=x
return sum/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
sum=0.0
if len(args)==0:
return sum
else:
for x in args:
sum+=x
return sum/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2020-01-27
def average(*args):
sum=0.0
i=0
if len(args)==0:
return sum
else:
for x in args:
sum+=x
i+=1
return sum/i
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
sum=0.0
i=0
if len(args)==0:
return sum
else:
for x in args:
sum+=x
i+=1
return sum/i
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2020-01-20
def greet(x=''):
if x=='':
print "Hello,world."
else:
print "Hello,"+x+'.'
greet()
greet('Bart')
if x=='':
print "Hello,world."
else:
print "Hello,"+x+'.'
greet()
greet('Bart')
2020-01-20
print [100*x + 10*y + z for x in range(1,10) for y in range(0,10) for z in range(0,10) if x == z]
2020-01-19
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
x = 0
while True:
if L[x] in s:
s.remove(L[x])
else:
s.add(L[x])
x = x + 1
if x >= 4:
break
print s
使用while-True + if-break语句虽然可以实现,但是和答案相比,明显代码多了许多。看来还需要多积累啊!
L = ['Adam', 'Lisa', 'Bart', 'Paul']
x = 0
while True:
if L[x] in s:
s.remove(L[x])
else:
s.add(L[x])
x = x + 1
if x >= 4:
break
print s
使用while-True + if-break语句虽然可以实现,但是和答案相比,明显代码多了许多。看来还需要多积累啊!
2020-01-16