鱼C论坛

 找回密码
 立即注册
查看: 3093|回复: 1

[已解决]为什么要输入多次exit才能退出啊?

[复制链接]
发表于 2023-2-15 13:54:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. oho = []
  2. #order = input("请输入指令(push/pop/top/exit):")
  3. def get_in():
  4.     while True:
  5.         order = input("请输入指令(push/pop/top/exit):")
  6.         if order == "push":
  7.             push()
  8.         elif order == "pop":
  9.             pop_()
  10.         elif order == "top":
  11.             top_filo()
  12.         else:
  13.             order == "exit"
  14.             break
  15.         break
  16.         
  17.    

  18. def print_filo():
  19.     print("栈:")
  20.     for i in oho:
  21.         print(i)
  22.     get_in()

  23. def push():
  24.     val = input("请输入将要压入栈中的值:")
  25.     oho.insert(0,val)
  26.     print_filo()
  27.     get_in()

  28. def top_filo():
  29.     if len(oho) >1:
  30.         print("栈:")
  31.         print(oho[0])
  32.     else:
  33.         print("栈已空")

  34. def pop_():
  35.     if len(oho) >1:
  36.         print(oho[0])
  37.         oho.pop(0)
  38.         print_filo()
  39.     else:
  40.         print("栈已空")
  41.    
  42. 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
>>>

最佳答案
2023-2-15 14:05:34
因为你递归调用了,把 print_filo 和 push 函数里面的 get_in 删掉

还有 while True 里面也不对,应该是 elif order == "exit",最后的那个 break 也删掉

  1. oho = []
  2. #order = input("请输入指令(push/pop/top/exit):")
  3. def get_in():
  4.     while True:
  5.         order = input("请输入指令(push/pop/top/exit):")
  6.         if order == "push":
  7.             push()
  8.         elif order == "pop":
  9.             pop_()
  10.         elif order == "top":
  11.             top_filo()
  12.         elif order == "exit":
  13.             break
  14.         else:
  15.             print("无效的指令")

  16.         
  17.    

  18. def print_filo():
  19.     print("栈:")
  20.     for i in oho:
  21.         print(i)


  22. def push():
  23.     val = input("请输入将要压入栈中的值:")
  24.     oho.insert(0,val)
  25.     print_filo()


  26. def top_filo():
  27.     if len(oho) >1:
  28.         print("栈:")
  29.         print(oho[0])
  30.     else:
  31.         print("栈已空")

  32. def pop_():
  33.     if len(oho) >1:
  34.         print(oho[0])
  35.         oho.pop(0)
  36.         print_filo()
  37.     else:
  38.         print("栈已空")
  39.    
  40. get_in()  
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-2-15 14:05:34 | 显示全部楼层    本楼为最佳答案   
因为你递归调用了,把 print_filo 和 push 函数里面的 get_in 删掉

还有 while True 里面也不对,应该是 elif order == "exit",最后的那个 break 也删掉

  1. oho = []
  2. #order = input("请输入指令(push/pop/top/exit):")
  3. def get_in():
  4.     while True:
  5.         order = input("请输入指令(push/pop/top/exit):")
  6.         if order == "push":
  7.             push()
  8.         elif order == "pop":
  9.             pop_()
  10.         elif order == "top":
  11.             top_filo()
  12.         elif order == "exit":
  13.             break
  14.         else:
  15.             print("无效的指令")

  16.         
  17.    

  18. def print_filo():
  19.     print("栈:")
  20.     for i in oho:
  21.         print(i)


  22. def push():
  23.     val = input("请输入将要压入栈中的值:")
  24.     oho.insert(0,val)
  25.     print_filo()


  26. def top_filo():
  27.     if len(oho) >1:
  28.         print("栈:")
  29.         print(oho[0])
  30.     else:
  31.         print("栈已空")

  32. def pop_():
  33.     if len(oho) >1:
  34.         print(oho[0])
  35.         oho.pop(0)
  36.         print_filo()
  37.     else:
  38.         print("栈已空")
  39.    
  40. get_in()  
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 01:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表