Hướng dẫn how to add more than two numbers in python - cách thêm nhiều hơn hai số trong python

Tôi mới đến Python và tôi bắt đầu học cách thực hiện chức năng. Và tôi bắt đầu thêm các số nhưng tôi chỉ có thể tổng hợp hai số và nếu tôi muốn tổng hợp nhiều hơn, nó sẽ yêu cầu tôi chỉnh sửa chương trình. Đây là mã của tôi

def sum[num1,num2]:
    return num1+num2

a=5
b=7
c=sum[a,b]
print [c]

Bây giờ tôi muốn tạo một hàm để tổng hợp bất kỳ số lượng nào bạn muốn mà không cần chỉnh sửa mã. Đây là những gì tôi đã làm:

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]

Điều này đã làm việc nhưng tôi nghĩ rằng nên có một cách dễ dàng hơn để làm điều này với một chức năng ... phải không? Cảm ơn bạn đã giúp đỡ!

Hỏi ngày 26 tháng 4 năm 2018 lúc 12:43Apr 26, 2018 at 12:43

0

Bạn muốn có một tham số

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
2 trong chức năng của mình để bạn có thể lấy nhiều đầu vào hơn 1:

def summ[num1, *args]:
    total = num1
    for num in args:
        total = total + num
    return total

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
2 có nghĩa là bạn có thể vượt qua nhiều đối số như bạn muốn:

>>> summ[1, 2, 3]
6
>>> summ[1, 2, 3, 4, 5, 6, 7]
28

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
4 là một đối tượng lặp: Chúng tôi lặp qua nó và thêm từng số của nó vào một
def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
5 mà sau đó chúng tôi quay lại.

Đã trả lời ngày 26 tháng 4 năm 2018 lúc 12:46Apr 26, 2018 at 12:46

TerryaterryaTerryA

57.4K11 Huy hiệu vàng117 Huy hiệu bạc140 Huy hiệu đồng11 gold badges117 silver badges140 bronze badges

7

Bạn có thể sử dụng biến ARG để chấp nhận bất kỳ số lượng đối số nào

def my_sum[*args]:
    total = 0
    for val in args:
        total += val
    return total

Bạn cũng có thể sử dụng tổng [] của Python để thêm chúng:

def my_sum[*args]:
    return sum[args]

>>> my_sum[1,2,3,4]
10
>>> my_sum[1,5,6,7.8]
19.8

Đã trả lời ngày 26 tháng 4 năm 2018 lúc 12:52Apr 26, 2018 at 12:52

Akshatakshatakshat

1.2091 Huy hiệu vàng8 Huy hiệu bạc22 Huy hiệu đồng1 gold badge8 silver badges22 bronze badges

0

Điều đầu tiên cần lưu ý là Python có chức năng

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
6 gốc. Sử dụng điều này thay vào đó để tính toán đơn giản, đừng ghi đè lên nó bằng riêng của bạn.

Nhưng để tìm hiểu thêm về Python, bạn có thể muốn sử dụng

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
7, áp dụng chức năng của hai đối số tích lũy cho các mục của chuỗi.

from functools import reduce

def mysum[*nums]:
    return reduce[lambda x, y: x+y, nums]

a, b, c = 1, 2, 3

res = mysum[a,b,c]  # 6

Điều này có thể được kết hợp vào logic của bạn bằng cách xây dựng một danh sách sau đó cung cấp cho chức năng của bạn:

lst = []

lst.append[int[input["what is the number you want to add?: "]]]

ask = "yes"

while ask == "yes":
    lst.append[int[input["what is the number you want to add?: "]]]
    ask = input["do you want to add another number?: "]
else:
    res = mysum[*lst]
    print[res]

Đã trả lời ngày 26 tháng 4 năm 2018 lúc 12:46Apr 26, 2018 at 12:46

Terryaterryajpp

57.4K11 Huy hiệu vàng117 Huy hiệu bạc140 Huy hiệu đồng32 gold badges261 silver badges320 bronze badges

2

Bạn có thể sử dụng biến ARG để chấp nhận bất kỳ số lượng đối số nào

Bạn cũng có thể sử dụng tổng [] của Python để thêm chúng:

def my_sum[*args]:
    return sum[args]

my_sum[1, 2, 3]

Đã trả lời ngày 26 tháng 4 năm 2018 lúc 12:52

my_sum[2] # result is 2
my_sum[]  # result is 0

Akshatakshat

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
0

Đã trả lời ngày 26 tháng 4 năm 2018 lúc 12:52Apr 26, 2018 at 12:52

0

Akshatakshat

  1. 1.2091 Huy hiệu vàng8 Huy hiệu bạc22 Huy hiệu đồng
  2. Điều đầu tiên cần lưu ý là Python có chức năng
    def sum[num1,num2]:
        return num1+num2
    a=int[input["what is the number you want to add?: "]]
    ask="yes"
    while[ask=="yes"]:
      b=int[input["what is the number you want to add?: "]]
      c=sum[a,b]
      a=c
      ask=input["do you want to add another number?: "]
    else:
        c=a
        print [c]
    
    6 gốc. Sử dụng điều này thay vào đó để tính toán đơn giản, đừng ghi đè lên nó bằng riêng của bạn.
  3. Nhưng để tìm hiểu thêm về Python, bạn có thể muốn sử dụng
    def sum[num1,num2]:
        return num1+num2
    a=int[input["what is the number you want to add?: "]]
    ask="yes"
    while[ask=="yes"]:
      b=int[input["what is the number you want to add?: "]]
      c=sum[a,b]
      a=c
      ask=input["do you want to add another number?: "]
    else:
        c=a
        print [c]
    
    7, áp dụng chức năng của hai đối số tích lũy cho các mục của chuỗi.
  4. Điều này có thể được kết hợp vào logic của bạn bằng cách xây dựng một danh sách sau đó cung cấp cho chức năng của bạn:

JPPJPP

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
1

153K32 Huy hiệu vàng261 Huy hiệu bạc320 Huy hiệu đồngApr 26, 2018 at 12:56

Có một số cách làm thế nào để thêm một số lượng số khác nhau. Trước hết, bạn chỉ có thể sử dụng một danh sách và tổng số chức năng tích hợp:

def sum[num1,num2]:
    return num1+num2
a=int[input["what is the number you want to add?: "]]
ask="yes"
while[ask=="yes"]:
  b=int[input["what is the number you want to add?: "]]
  c=sum[a,b]
  a=c
  ask=input["do you want to add another number?: "]
else:
    c=a
    print [c]
8Laurent H.

Nếu bạn không muốn sử dụng danh sách, hãy thử viết một trình bao bọc tùy chỉnh trên hàm tổng tích hợp [thực tế là không ghi đè lên các tên tích hợp]:1 gold badge17 silver badges39 bronze badges

2

Bài Viết Liên Quan

Chủ Đề