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
- Python được cài đặt trên máy tính của bạn
- 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
3 hoạt động như thế nàoamount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
- Sử dụng
4 để chấp nhận đầu vào sốamount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
- Giới thiệu về vòng lặp
5amount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
- Sử dụng vòng lặp
5 với câu lệnhamount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
7amount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
- Thoát khỏi vòng lặp
5 bằng breakamount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
- 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
5amount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
- Điền từ điển với đầu vào của người dùng bằng vòng lặp
5amount = input["Please enter your amount? "] rate=0.056 interest= amount*rate print["Your interest is : "] print[interest]
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ớiVí 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ùngSử 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 đâyamount = 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ướiamount = 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]
1Khi 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ãiGiớ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 đúngcú pháp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4Vò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 15amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
6Vò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 15Sử 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 saiVí 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]
5amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
2Chươ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 thiKết quả
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
4Sử 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ôngcú pháp
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
8Thí dụ
amount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
9Xó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]
5Giả 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ướiamount = 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ểnamount = input["Please enter your amount? "]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[interest]
1Khi 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]
2Phầ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