函数中列表的总和
Sum of lists in a function
我有一个关于 python 'functions' 编程的问题。
这是我的脚本:
def print_seat(seat):
for item in seat:
print "${}".format(item)
print "-"*15
total = get_seat_total(seat)
print "Total: ${}".format(total)
def get_seat_total(seat):
total = 0
for dish in seat:
total += dish
return total
def main():
seats = [[19.95], [20.45 + 3.10], [7.00/2, 2.10, 21.45], [7.00/2, 2.10, 14.99]]
grand_total = 0
for seat in seats:
print_seat(seat)
grand_total += get_seat_total(seat)
print "\n"
print "="*15
print "Grand total: ${}".format(grand_total)
if __name__ == "__main__":
main()
这是我的脚本结果:
.95
-----------
Total: .95
.55
-----------
Total: .55
.5
.1
.45
------------
Total: .5
.5
.1
.99
------------
Total: .5
============
Grand total: .5
但是脚本的结果应该是这样的:
.95
-----------
Total: .95
.55
-----------
Total: .55
.5
.1
.45
------------
Total: .05
.5
.1
.99
------------
Total: .59
============
Grand total: .14
从上面可以看出,列表的总数是不同的。我想我写的所有内容都是正确的,包括列表的总和(如果我没记错的话)。有人可以指出我的脚本结构有什么问题吗?还是我脚本写错了?
问题在于,在您的 get_seat_total()
函数中,您是从循环内部 returning 的,因此它会 return 仅添加第一项后的总数。你应该只在循环完成后 return,示例 -
def get_seat_total(seat):
total = 0
for dish in seat:
total += dish
return total
希望对您有所帮助,
def print_seat(seat):
for item in seat:
print "${}".format(item)
print "-"*15
total = sum(seat)
print "Total: ${}".format(total)
def main():
seats = [[19.95], [20.45 + 3.10], [7.00/2, 2.10, 21.45], [7.00/2, 2.10, 14.99]]
grand_total = 0
for seat in seats:
print_seat(seat)
grand_total += sum(seat)
print "\n"
print "="*15
print "Grand total: ${}".format(grand_total)
if __name__ == "__main__":
main()
最佳,
我有一个关于 python 'functions' 编程的问题。
这是我的脚本:
def print_seat(seat):
for item in seat:
print "${}".format(item)
print "-"*15
total = get_seat_total(seat)
print "Total: ${}".format(total)
def get_seat_total(seat):
total = 0
for dish in seat:
total += dish
return total
def main():
seats = [[19.95], [20.45 + 3.10], [7.00/2, 2.10, 21.45], [7.00/2, 2.10, 14.99]]
grand_total = 0
for seat in seats:
print_seat(seat)
grand_total += get_seat_total(seat)
print "\n"
print "="*15
print "Grand total: ${}".format(grand_total)
if __name__ == "__main__":
main()
这是我的脚本结果:
.95
-----------
Total: .95
.55
-----------
Total: .55
.5
.1
.45
------------
Total: .5
.5
.1
.99
------------
Total: .5
============
Grand total: .5
但是脚本的结果应该是这样的:
.95
-----------
Total: .95
.55
-----------
Total: .55
.5
.1
.45
------------
Total: .05
.5
.1
.99
------------
Total: .59
============
Grand total: .14
从上面可以看出,列表的总数是不同的。我想我写的所有内容都是正确的,包括列表的总和(如果我没记错的话)。有人可以指出我的脚本结构有什么问题吗?还是我脚本写错了?
问题在于,在您的 get_seat_total()
函数中,您是从循环内部 returning 的,因此它会 return 仅添加第一项后的总数。你应该只在循环完成后 return,示例 -
def get_seat_total(seat):
total = 0
for dish in seat:
total += dish
return total
希望对您有所帮助,
def print_seat(seat):
for item in seat:
print "${}".format(item)
print "-"*15
total = sum(seat)
print "Total: ${}".format(total)
def main():
seats = [[19.95], [20.45 + 3.10], [7.00/2, 2.10, 21.45], [7.00/2, 2.10, 14.99]]
grand_total = 0
for seat in seats:
print_seat(seat)
grand_total += sum(seat)
print "\n"
print "="*15
print "Grand total: ${}".format(grand_total)
if __name__ == "__main__":
main()
最佳,