英文:
Jump from inner if statement to outer else
问题
我有一段代码,多次评估给定的值 a
和 b
。如果满足条件,会执行不同的操作。有可能在改变 b
后,条件不再满足,这时应该按照 else
部分的说明进行逻辑处理。
以下是算法的一个简化部分:
if a > b:
# 做一些事情 0
b += x # 改变 b 的语句
if a > b:
# 做一些事情 1
else:
# 做一些事情 2
# 现在 b > a,因此它匹配第一个 if 语句,并应该跳转到第一个 else
else:
# 做一些事情 2
如何在不在多个 else
下重复编写相同的代码来实现这一点?
英文:
I have a code that evaluates the given values a
and b
several times. If a condition is matched, different stuff is done. It can be the case that after varying b
, the condition is unmatched and then, in a logical way, it should do what was stated before in the else
.
Here's a simplified part of the algorithm:
if a > b:
# do stuff 0
b += x # statement that varies b
if a > b:
# do stuff 1
else:
# do stuff 2
# now b > a so it matches the first if statement and should jump to first else
else:
# do stuff 2
How can I do that without writing the same piece of code under several elses
?
答案1
得分: 3
不要嵌套语句;检查是否 a > b
,然后检查在可能更新 b
后 a
是否仍然大于 b
。
# 如果这是真的,假设 x 是非负的,a > b 也是真的
if a > b:
b = b + x
if a > b:
# 做事情 1
else:
# 做事情 2
如果第一个 a > b
失败,第二个也会失败。
英文:
Don't nest the statements; check if a > b
, then check if a
is still greater than b
after the possible update of b
.
# If this is true, a > b is true (assuming x is nonnegative)
if a > b:
b = b + x
if a > b:
# do stuff 1
else:
# do stuff 2
If the first a > b
failed, the second one will as well.
答案2
得分: 1
你可以按照文档中的说明模拟一个GoTo
语句:
class Stuff2(Exception): pass
try:
a = 2
b = 1
if a > b:
print("Doing stuff 0")
x = int(input("Enter the value of x: "))
b += x
if a > b:
print("Doing stuff 1")
else:
raise Stuff2()
else:
raise Stuff2()
except Stuff2:
print("Doing stuff 2")
测试/输出:
Doing stuff 0
Enter the value of x: 2
Doing stuff 2
英文:
You can simulate a GoTo
statement as per the docs :
class Stuff2(Exception): pass
try:
a = 2
b = 1
if a > b:
print("Doing stuff 0")
x = int(input("Enter the value of x: "))
b += x
if a > b:
print("Doing stuff 1")
else:
raise Stuff2()
else:
raise Stuff2()
except Stuff2:
print("Doing stuff 2")
Test/Output :
Doing stuff 0
Enter the value of x: 2
Doing stuff 2
答案3
得分: 0
I'm not sure if this can help you and I understand your questions correctly, use a variable with boolean True/False, change the status inside and check it outside. Set it to a default value at the start.
have_to_do_stuff_2 = False
if a > b:
print("a > b")
b += x # statement that varies b
if a > b:
# do stuff 1
else:
# do stuff 2
# now b > a, so it matches the first if statement and should jump to the first else
have_to_do_stuff_2 = True
else:
have_to_do_stuff_2 = True
if have_to_do_stuff_2:
# do stuff 2
英文:
Im not sure if this can helps you and I understand correctly your questions, use a variable with boolean True/False, change status inside and check it outside. Set it a default value at start
have_to_do_stuff_2 = False
if a > b:
print("a > b")
b += x # statement that varies b
if a > b:
# do stuff 1
else:
# do stuff 2
# now b > a so it matches the first if statement and should jump to first else
have_to_do_stuff_2 = True
else:
have_to_do_stuff_2 = True
if have_to_do_stuff_2:
# do stuff 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论