Python 不间断地缩短
因此,基本上正如标题所说,我想知道是否有一种方法可以在不使用break语句的情况下缩短它 你是说:Python 不间断地缩短,python,python-3.x,Python,Python 3.x,因此,基本上正如标题所说,我想知道是否有一种方法可以在不使用break语句的情况下缩短它 你是说: fun = input("Enter 1 or 2:") if fun == '1': print("Programming is fun!") elif fun == '2': print("You're getting the hang of this.") elif fun == 'bye': print("Bye, bye.") else: print("Sorry
fun = input("Enter 1 or 2:")
if fun == '1':
print("Programming is fun!")
elif fun == '2':
print("You're getting the hang of this.")
elif fun == 'bye':
print("Bye, bye.")
else:
print("Sorry that isn't a 1 or 2.")
while fun != 'bye':
fun = input("Enter 1 or 2:")
if fun == '1':
print("Programming is fun!")
elif fun == '2':
print("You're getting the hang of this.")
elif fun == 'bye':
print("Bye, bye.")
else:
print("Sorry that isn't a 1 or 2.")
当然,有了Python 3.8中新的“walrus”操作符,您可以使用:
fun = ""
while fun != 'bye':
fun = input("Enter 1 or 2:")
if fun == '1':
print("Programming is fun!")
elif fun == '2':
print("You're getting the hang of this.")
elif fun == 'bye':
print("Bye, bye.")
else:
print("Sorry that isn't a 1 or 2.")
使用break是一种非常有效的编程技术,完全可以用来编写简洁、性能好、易于使用的代码。说它很糟糕是一种不真实的笼统说法。你是对的,你也可以在这个例子中使用它 但是,您仍然可以通过使用单个
while
循环并将所有逻辑放在其中,来缩短代码,而不使用break
。例如,见下文:
while (fun := input("Enter 1 or 2:")) != 'bye':
...
其他回答回答了您的具体问题,并详细说明了如何避免
返回
和中断
,但由于您可能会阅读其他人的代码,请知道通常我们只需编写:
fun = ''
while fun != 'bye':
fun = input("Enter 1 or 2:")
if fun == '1':
print("Programming is fun!")
elif fun == '2':
print("You're getting the hang of this.")
elif fun == 'bye':
print("Bye, bye.")
else:
print("Sorry that isn't a 1 or 2.")
海象版本:
while True: # <--- No check before entering the loop.
fun = input("Enter 1 or 2:")
# If the input is a text, check if it's a variant of "bye".
if fun.lower() == 'bye':
print("Bye, bye.")
break
# Otherwise check if it's 1 or 2.
else:
# If the conversion to integer fails, print error message.
try:
fun = int(fun)
except ValueError:
print("Sorry that isn't a 1 or 2.")
# If it works, handle as usual.
if fun == 1:
print("Programming is fun!")
elif fun == 2:
print("You're getting the hang of this.")
另一个变化:
while (fun := input("Enter 1 or 2:")) != 'bye':
if fun == '1':
print("Programming is fun!")
elif fun == '2':
print("You're getting the hang of this.")
else:
print("Sorry that isn't a 1 or 2.")
print("Bye, bye.")
不确定您是否可以使用字典,但它们非常适合以下情况:
replies = {'1': "Programming is fun!",
'2': "You're getting the hang of this."}
while (fun := input("Enter 1 or 2:")) != 'bye':
print(replies.get(fun, "Sorry that isn't a 1 or 2."))
print("Bye, bye.")
使用
dict.get
让我们提供一个默认值作为第二个参数,它本质上是一个else语句。为什么不使用break
?我目前是编程专业的一年级学生,如果这是一个愚蠢的问题,很抱歉?感谢所有的帮助。找一个新老师,你有没有一个版本使用更短的休息时间?你应该让我们看看。另外,对于任何与python相关的应用程序,请始终使用generic[python]标记quesiton@Blackasnight69听到你的老师这么说有点失望,因为break
是一种非常有效的早期跳转技巧。现在,如果你真的想避免它,你可以把循环放在一个函数中,用return
来代替。啊,新的walrus操作符真是妙用!这是一个很好的使用示例。看这是我最初写的,但他不想让我使用break@Blackasnight69我猜他想禁止使用break让你研究其他解决方案,这没关系,但只要知道你第一次就做对了:)
replies = {'1': "Programming is fun!",
'2': "You're getting the hang of this."}
while (fun := input("Enter 1 or 2:")) != 'bye':
print(replies.get(fun, "Sorry that isn't a 1 or 2."))
print("Bye, bye.")
fun = ""
choices = {'1': "Programming is fun!",
'2': "You're getting the hang of this.",
'bye': "Bye, bye."}
while fun != 'bye':
fun = input("Enter 1 or 2:")
print(choices.get(fun.lower(), "Sorry that isn't a 1 or 2.")