[英]How to break a loop over user input
我最近開始研究 python 並且我遇到了這個問題。 我需要打破用戶輸入的循環。 我現在需要的幾乎是:當它達到 cat_count 時打破grams_per_cat 輸入循環。 幾乎練習是 cat_count 是貓的數量,每只貓的克數是每只貓每天吃多少。 所以當它擊中時,例如 cat_count 是 6 所以在 6 個輸入之后我想打破輸入循環。
cat_count = int(input())
while True:
grams_per_cat = int(input())
定義一個計數器並在特定條件下使用它來中斷:
cat_count = int(input())
# Our counter
i = 0
while True:
if i >= cat_count:
# Interrupt the loop if we reached cat_count
break
grams_per_cat = int(input())
# Increase counter
i += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.