d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for name, score in d.items():
sum = sum + score
print name , ':', score
print 'average', ':', sum/len(d)
sum = 0.0
for name, score in d.items():
sum = sum + score
print name , ':', score
print 'average', ':', sum/len(d)
2019-10-28
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for v in d.values():
sum = sum + v
print sum /len(d)
sum = 0.0
for v in d.values():
sum = sum + v
print sum /len(d)
2019-10-28
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L):
print index+1, '-', name
for index, name in enumerate(L):
print index+1, '-', name
def firstCharUpper(s):
for i in s:
if i==s[0]:
s=s[0].upper()+s[1:]
return s
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
for i in s:
if i==s[0]:
s=s[0].upper()+s[1:]
return s
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2019-10-28
L = range(1, 101)
for i in L:
i+=i
print L[0:10]
print L[2:101:3]
print L[4:51:5]
for i in L:
i+=i
print L[0:10]
print L[2:101:3]
print L[4:51:5]
2019-10-28
def average(*args):
if len(args)== 0:
return '0.0'
else:
return float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if len(args)== 0:
return '0.0'
else:
return float(sum(args))/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2019-10-28
最赞回答 / 從頭再戰
我是这么理解的,代码改成t = ('a', 'b', 'A', 'B')就变成了包含四个元素了,而t = ('a', 'b', ['A', 'B'])只包含三个元素
2019-10-27
最新回答 / 慕虎9037580
因为你运行完第一个print的时候L1=[55],运行下一个print把这个55给加进去了。下面这么写就可以了def square_of_sum(L): L1 = [] for x in L: a = x L1.append(a*a) return sum(L1)print square_of_sum([1, 2, 3, 4, 5])print square_of_sum([-5, 0, 5, 15, 25])
2019-10-27
#改进一下
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
2019-10-27
#改进一下
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
import math
def quadratic_equation(a,b,c):
if b**2-4*a*c>=0:
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
else:
return '根为空'
2019-10-27
import math
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-a*c*4))/(a*2)
x2=(-b-math.sqrt(b**2-a*c*4))/(a*2)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-a*c*4))/(a*2)
x2=(-b-math.sqrt(b**2-a*c*4))/(a*2)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2019-10-27