Jump out of the multilayer loop: three-layer loop, the innermost layer directly jumps out 3 layers
In Python, the function runs to return and stops, so you can use this feature to write functions as functions to terminate multiple loops
1234567891011121314 |
def
work():
for
i
in
range
(
5
):
print
(
"i="
, i)
for
j
in
range
(
5
):
print
(
"--j="
, j)
for
k
in
range
(
5
):
if k<
2
:
print
(
"------>k="
, k)
else
:
return i,j,k
print
(work())
|
Note: The following updates are extended for this link:
Http://www.cnblogs.com/xuyaping/p/6639830.html
Shopping Cart Procedure
Requirements: 1. After starting the program, let the user enter the payroll and then print the list of items
2. Allow users to purchase items based on their product number
3. After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
4. Can exit at any time, when you exit, print purchased goods and balances
1234567891011121314151617181920212223 |
salary
=
int
(
input
(
"input your salary:"
))
#输入工资
balance
=
salary
#将工资赋值给余额
products
=
[
"book"
,
"coffe"
,
"cake"
,
"iphone"
]
#定义可购买商品
price
=
[
40
,
20
,
30
,
2000
]
#定义商品价格
print
(
"number "
,
"products "
,
"price"
)
#输出商品列表第一行
products_buy
=
[]
#设置购物车商品列表
for
i
in
range
(
len
(products)):
#输出商品列表产品及价格
print
(i
+
1
,
"\t\t"
,products[i],
"\t\t"
, price[i])
print
(
"q"
,
"\t\t"
,
"quit"
)
#输出“q”指令打印命令“quit”(退出)
while
True
:
number
=
input
(
"input your products_number:"
)
if
number!
=
"q"
:
#若输入的不是“q”,则为整型,循环打印已购买商品和余额
number_
=
int
(number)
if
balance >
=
price[number_
-
1
]:
balance
-
=
price[number_
-
1
]
products_buy.append(products[number_
-
1
])
print
(
"products_buy is"
,products_buy,
",balance is "
,balance)
else
:
print
(
"余额不足!"
)
else
:
#输入的为“q”,打印已购买商品和余额
print
(
"products_buy is"
, products_buy,
",balance is "
, balance)
break
|
To run the program:
Python practice-jump out of multi-layered loops and shopping carts