马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
oho = []
#order = input("请输入指令(push/pop/top/exit):")
def get_in():
while True:
order = input("请输入指令(push/pop/top/exit):")
if order == "push":
push()
elif order == "pop":
pop_()
elif order == "top":
top_filo()
else:
order == "exit"
break
break
def print_filo():
print("栈:")
for i in oho:
print(i)
get_in()
def push():
val = input("请输入将要压入栈中的值:")
oho.insert(0,val)
print_filo()
get_in()
def top_filo():
if len(oho) >1:
print("栈:")
print(oho[0])
else:
print("栈已空")
def pop_():
if len(oho) >1:
print(oho[0])
oho.pop(0)
print_filo()
else:
print("栈已空")
get_in()
各位老师,请问下为什么要多次输入exit才能退出啊?
请输入指令(push/pop/top/exit):push
请输入将要压入栈中的值:520
栈:
520
请输入指令(push/pop/top/exit):exit
请输入指令(push/pop/top/exit):exit
>>>
==================== RESTART: C:\Users\cjc04\Desktop\temp.py ===================
请输入指令(push/pop/top/exit):push
请输入将要压入栈中的值:520
栈:
520
请输入指令(push/pop/top/exit):push
请输入将要压入栈中的值:521
栈:
521
520
请输入指令(push/pop/top/exit):exit
请输入指令(push/pop/top/exit):exit
请输入指令(push/pop/top/exit):exit
>>>
因为你递归调用了,把 print_filo 和 push 函数里面的 get_in 删掉
还有 while True 里面也不对,应该是 elif order == "exit",最后的那个 break 也删掉
oho = []
#order = input("请输入指令(push/pop/top/exit):")
def get_in():
while True:
order = input("请输入指令(push/pop/top/exit):")
if order == "push":
push()
elif order == "pop":
pop_()
elif order == "top":
top_filo()
elif order == "exit":
break
else:
print("无效的指令")
def print_filo():
print("栈:")
for i in oho:
print(i)
def push():
val = input("请输入将要压入栈中的值:")
oho.insert(0,val)
print_filo()
def top_filo():
if len(oho) >1:
print("栈:")
print(oho[0])
else:
print("栈已空")
def pop_():
if len(oho) >1:
print(oho[0])
oho.pop(0)
print_filo()
else:
print("栈已空")
get_in()
|