Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Em muốn input nhiều số một lúc, ngăn cách bởi ', ' (phẩy cách) thì phải làm ntn?

3 năm trước 544 lượt xem 5 bình luận

Ví dụ:
toa_do.append(float(input("Nhap toa do [Ax, Ay, Bx, By, Cx, Cy]: ")))

rồi user nhập '1, 2, 3, 4, 5, 7'

Phải code thế nào ạ?

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
chippython1 đã bình luận 3 năm trước

Dễ thế mà ko làm đc??

 

leducanh1290 đã bình luận 3 năm trước

Cho user nhập cả chuỗi đấy rồi dùng splits để tách ra thành arrays input truyền vào là ",".

tmh184 đã bình luận 3 năm trước

Em làm được rồi, kiểu như thế này:

entered_list = (input("Enter a list of numbers separated by comma-space: ").split(', '))
print('Intermediate_list: ',entered_list)

num_list = list(map(float,entered_list))
print('Number List: ',num_list)
print('List sum:', sum(num_list))

Code này em sửa lại ở đây một tí (nguồn: https://towardsdatascience.com/a-complete-guide-to-user-input-in-python-727561fc16e1#:~:text=Python%20provides%20a%20simple%20framework%20for%20getting%20user%20input.,input%20into%20the%20required%20format.)

Câu hỏi mới nhất