Cách lấy n đầu vào trong một dòng trong python bằng vòng lặp for

Là một lập trình viên, nhiều chương trình bạn sẽ viết sẽ giải quyết vấn đề của người dùng cuối. Bạn sẽ luôn muốn lấy một số dữ liệu từ người dùng. Bài viết này sẽ giải thích cách xử lý đầu vào của người dùng và cách sử dụng vòng lặp while

điều kiện tiên quyết

  1. Python được cài đặt trên máy tính của bạn
  2. Kiến thức Python cơ bản

mục tiêu

Trong bài này chúng ta sẽ đi qua

  • Hàm
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    3 hoạt động như thế nào
  • Sử dụng
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    4 để chấp nhận đầu vào số
  • Giới thiệu về vòng lặp
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    5
  • Sử dụng vòng lặp
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    5 với câu lệnh
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    7
  • Thoát khỏi vòng lặp
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    5 bằng break
  • Xóa tất cả các trường hợp của các giá trị cụ thể khỏi danh sách bằng vòng lặp
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    5
  • Điền từ điển với đầu vào của người dùng bằng vòng lặp
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    5

Cách thức hoạt động của hàm input[]

Hàm

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
3 tạm dừng thực thi chương trình và đợi người dùng nhập một số dữ liệu. Khi Python nhận đầu vào của người dùng, nó sẽ lưu đầu vào trong biến mà bạn chọn để làm việc với

Ví dụ: hãy tạo một chương trình chấp nhận tên người dùng và in lại tên

name = input["Please enter your name: "]
print[name]
#output
#Please enter your name: John
#John

Hàm

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
3 nhận một đối số, nghĩa là hướng dẫn bạn muốn người dùng xem. Trong ví dụ này, Python thực thi dòng đầu tiên và yêu cầu người dùng nhập tên của họ. Việc thực thi chương trình sẽ tạm dừng và chờ người dùng nhập tên của mình và tiếp tục sau khi nhấn phím
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
13 trên bàn phím. Sau đó, tên của người dùng được tải vào biến
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
14, sau đó
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
15 hiển thị lại tên người dùng

Sử dụng int[] để chấp nhận đầu vào số

Bất kỳ văn bản nào người dùng nhập bằng hàm

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
3, được hiểu là một chuỗi. Nếu chỉ cần in ra kết quả nhập thì dùng hàm
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
3 là đủ. Sử dụng đầu vào trực tiếp dưới dạng giá trị số sẽ tạo ra một ngoại lệ. Hãy xem xét ví dụ dưới đây

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]

Ví dụ trên đưa ra lỗi vì Python hiểu đầu vào

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
18 là một chuỗi. Python không thể nhân một chuỗi và float

Để giải quyết vấn đề trên, chúng tôi sử dụng hàm

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4 thông báo cho Python sử dụng đầu vào dưới dạng giá trị số. Hàm
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4 chuyển đổi chuỗi thành số nguyên, được hiển thị bên dưới

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
1

đầu ra

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
1

Khi chúng ta nhập 9000 trong ví dụ trên, Python hiểu nó là một chuỗi. Số tiền này sau đó được chuyển đổi thành số nguyên bằng cách sử dụng hàm

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4. Bây giờ Python tính toán giá trị của tiền lãi

Giới thiệu vòng lặp while

Trong python, một vòng lặp

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5 thực thi một khối mã nhất định với điều kiện là một số điều kiện vẫn đúng

cú pháp

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4

Vòng lặp

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5 sau đếm từ 10 đến 15

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
6

Vòng lặp

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5 tiếp tục thực hiện nếu giá trị của
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
15 bằng hoặc nhỏ hơn 15

Sử dụng câu lệnh else với vòng lặp while

Khi bạn sử dụng câu lệnh

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
7 trong vòng lặp while, nó sẽ chỉ thực thi khi điều kiện trở thành sai

Ví dụ dưới đây liên quan đến cả câu lệnh

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
7 và câu lệnh
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
2

Chương trình trên in ra một số nhỏ hơn hoặc bằng 15, nếu không thì khối

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
7 thực thi

Kết quả

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4

Sử dụng break để thoát khỏi vòng lặp

Sử dụng câu lệnh

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
40 để thoát khỏi vòng lặp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5 mà không thực thi bất kỳ mã nào còn lại trong vòng lặp. Với câu lệnh
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
40, bạn có thể kiểm soát dòng nào trong chương trình của mình sẽ thực thi hay không

cú pháp

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
8

Thí dụ

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
9

Xóa tất cả các trường hợp của các giá trị cụ thể khỏi danh sách bằng vòng lặp while

Phương thức

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
43 chỉ xóa một giá trị khỏi danh sách. Chúng tôi loại bỏ tất cả các trường hợp của một giá trị khỏi danh sách bằng cách sử dụng vòng lặp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5

Giả sử chúng ta có một danh sách nhân viên và tên

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
45 xuất hiện nhiều lần. Chúng tôi có thể loại bỏ tất cả các phiên bản của tên
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
45 bằng cách sử dụng vòng lặp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5. Vòng lặp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5 thực hiện cho đến khi
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
45 không còn trong danh sách như minh họa bên dưới

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
0

Điền từ điển với đầu vào của người dùng bằng vòng lặp while

Chúng tôi sử dụng vòng lặp

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5 để nhắc người dùng nhập bao nhiêu thông tin đầu vào mà chúng tôi cần. Hãy tạo một chương trình chấp nhận tên người dùng và tên của ngọn núi mà mỗi người dùng thích leo lên. Vì chúng tôi muốn kết nối từng phản hồi với một người dùng cụ thể, chúng tôi sẽ lưu trữ dữ liệu trong từ điển

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
1

Khi bạn thực hiện chương trình này và nhập một số câu trả lời, đầu ra sẽ như thế này

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
2

Phần kết luận

Bây giờ bạn đã học cách sử dụng hàm

amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
3 và vòng lặp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
5, hãy thử triển khai chúng trong chương trình Python của bạn

Làm cách nào để lấy danh sách đầu vào trong Python trong một dòng bằng vòng lặp for?

Nhập danh sách bằng hàm input[] và range[] .
Đầu tiên, tạo một danh sách trống
Tiếp theo, chấp nhận kích thước danh sách từ người dùng [tôi. e. , số phần tử trong một danh sách]
Chạy vòng lặp cho đến hết kích thước của danh sách bằng cách sử dụng hàm for loop và range[]
sử dụng hàm input[] để nhận số từ người dùng

Làm cách nào để lấy nhiều đầu vào trong Python bằng vòng lặp for trong một dòng?

Để lấy nhiều đầu vào bằng vòng lặp for. .
Khai báo một biến mới và khởi tạo nó vào một danh sách trống
Sử dụng lớp range[] để lặp N lần trong vòng lặp for
Trên mỗi lần lặp lại, hãy thêm đầu vào của người dùng vào danh sách

Chủ Đề