Python 如何清除用户输入?

Python 如何清除用户输入?,python,while-loop,Python,While Loop,所以我用数字建立了垄断贸易体系。例如,如果我想添加一些东西,我输入数字1,如果我想删除一些东西,我输入数字2,等等。我的问题是,如果我退出while循环,基本上是代码“break”,之前输入的break也会激活主菜单的if命令。如果你不明白我想说什么,我不知道我是否可以在这个网站上发布链接,但是链接是: 如果操作==2: 尽管如此: replit.clear() 打印(“您希望提供什么?\n1:Add\n2:Remove\n3:Clear\n4:Offer Trade\n5:Cancel\n6

所以我用数字建立了垄断贸易体系。例如,如果我想添加一些东西,我输入数字1,如果我想删除一些东西,我输入数字2,等等。我的问题是,如果我退出while循环,基本上是代码“break”,之前输入的break也会激活主菜单的if命令。如果你不明白我想说什么,我不知道我是否可以在这个网站上发布链接,但是链接是:

如果操作==2:
尽管如此:
replit.clear()
打印(“您希望提供什么?\n1:Add\n2:Remove\n3:Clear\n4:Offer Trade\n5:Cancel\n6:Switch”)
Action=int(输入(“>>>”)
如果操作==1:
尽管如此:
replit.clear()
打印(“您想添加什么?\n1:货币\n2:财产\n3:免监禁卡\n4:背面”)
Action=int(输入(“>>>”)
如果操作==1:
如果匝数=1:
尽管如此:
replit.clear()
打印(“您想为交易支付多少钱?\n货币:”,str(Player1Money))
Action=int(输入(“>>>”)

如果Action>=0且Action=0且Action,则应实现单个
While True
循环,这将重置每个循环的用户输入(只要在循环内覆盖它)!我还建议您将代码重新创建为函数,而不是几个嵌套的逻辑语句。在每个循环中获取用户输入的代码示例(根据您的问题清除用户输入):

对于示例输出:

What would you like to do?
    1) say hi
    2) say bye
    3) pay taxes
    4) ???
    5) profit
    6) Exit D:
    >>>1
Hi!
What would you like to do?
    1) say hi
    2) say bye
    3) pay taxes
    4) ???
    5) profit
    6) Exit D:
    >>>6

Process finished with exit code 0
打开一个交互式终端并运行
import this
,查看Python的Zen也是一个好主意:

>>>import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
特别值得注意的是,我们发现以下几点适用:

平的比嵌套的好

可读性很重要


维护一个while循环对这两个方面都有很大帮助。在代码中的某一点上,您会被困在3
中,而True:
循环,然后尝试用单个
中断来中断多个
循环,但始终会被困在至少两个级别的深处…

欢迎使用堆栈溢出。我建议您启动一个新的python文件,并创建一个简单的示例来说明与完整程序相同的问题。我还建议您学习函数和类。随着代码变得越来越复杂,这些工具可以帮助您组织代码。欢迎使用StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确指定问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。请看。顺便说一句,尝试将代码拆分为函数,因为几个月后,你宁愿死也不愿找到应用所需编辑的位置……这段代码让我非常害怕。链接中的代码更糟糕。我认为您应该花一些时间来了解列表和函数。他们将使这项任务容易得多。
>>>import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!