python - 代码结束时在 Python 中再次启动函数
问题描述
我创建了一些代码
def Basket(): #the start of the code
items1=[]
items1=input("type items\n")
options=int(input("choose options\n"))
if options==1:
print("items on basket are:\n", items1)
elif options==2:
print(items1.count(',')+1) #the end
Basket()
我想一次又一次地使用该程序而不关闭它,所以我需要程序在结束帮助后始终转到代码的开头?
解决方案
简单地 :
def Basket(): #the start of the code
while True:
items1=[]
items1=input("type items\n")
options=int(input("choose options\n"))
if options==1:
print("items on basket are:\n", items1)
elif options==2:
print(items1.count(',')+1) #the end
Basket()
或者(正如@zvone 评论的那样)
def Basket(): #the start of the code
items1=[]
items1=input("type items\n")
options=int(input("choose options\n"))
if options==1:
print("items on basket are:\n", items1)
elif options==2:
print(items1.count(',')+1) #the end
while True: Basket()
或(优雅地)
def Basket(): #the start of the code
while input("add an item ? (y:Yes or n:No) \n") == "y":
items1=[]
items1=input("type items\n")
options=int(input("choose options\n"))
if options==1:
print("items on basket are:\n", items1)
elif options==2:
print(items1.count(',')+1) #the end
Basket()
推荐阅读
- .net-core - 积极的 F# 编译器优化是否只发生在引用的依赖项 + 发布配置上?
- elasticsearch - 弹性搜索 - 节点故障
- discord.net - Discord.net 如何提及角色
- powerbi - 是否可以将 2 个或 3 个 SSAS Cube 连接到 Power bi 并创建视图?
- javascript - 位置问题:包含按钮的粘性页脚
- php - 如何使用 PHP 抓取单个linkedin 帖子
- html - 动态 SVG 模式作为背景:可能吗?我举个例子
- c++ - 错误:从 'std::__1::__wrap_iter 没有可行的转换
'到'int' - c - 数组中的二分查找
- javascript - NodeJS API 获取每个类别的 Items 数组