Hướng dẫn how do you input a loop in python? - làm thế nào để bạn nhập một vòng lặp trong python?

Là một lập trình viên, nhiều chương trình mà 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 nhận 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 trong khi các vòng lặp.

Đ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 viết này, chúng tôi sẽ trả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ề
    amount = input["Please enter your amount? "]
    rate=0.056
    interest= amount*rate
    print["Your interest is : "]
    print[interest]
    
    5 Loops
  • 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 cách sử dụ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 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
  • Đổ đầy từ điển với đầu vào của người dùng 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

Hoạt động của hàm input [] hoạt động

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 việc thực hiện chương trình và chờ người dùng chính trong một số dữ liệu. Khi Python nhận được đầu vào của người dùng, nó sẽ lưu trữ đầu vào trong biến mà bạn chọn làm việc.

Ví dụ: hãy để Lừa 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 có trong 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 hiện dòng đầu tiên và yêu cầu người dùng nhập tên của mình. Việc thực hiện chương trình sẽ dừng lại 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? "]
amount=int[amount]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[ interest]
3 trên bàn phím. Tên của người dùng sau đó được tải trong biến
amount = input["Please enter your amount? "]
amount=int[amount]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[ interest]
4, sau đó
amount = input["Please enter your amount? "]
amount=int[amount]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[ interest]
5 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 mà người dùng đầu vào sử dụ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 bạn chỉ cần in ra đầu vào thì sử 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 làm giá trị số ném một ngoại lệ. 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 một lỗi vì Python diễn giải đầu vào

amount = input["Please enter your amount? "]
amount=int[amount]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[ interest]
8 dưới dạng chuỗi. Python có thể nhân lê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 làm 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? "]
amount=int[amount]
rate=0.056
interest= amount*rate
print["Your interest is : "]
print[ interest]

Output:

Please enter your amount? 9000
Your interest is : 
504.0

Khi chúng ta nhập 9000 vào ví dụ trên, Python diễn giải nó dưới dạng chuỗi. Số tiền sau đó được chuyển đổi thành một số nguyên bằ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 trong khi các vòng lặp

Trong Python, 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à điều kiện nhất định vẫn đúng.

Syntax:

while expression:
 statement[s]

Các vòng lặp

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

count = 10
while [count 

Bài Viết Liên Quan

Chủ Đề