如果别的 [英] if - else

查看:66
本文介绍了如果别的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重写用VB编写的旧程序。我无法弄清楚如何执行以下操作:


如果SumOfNumbers == 11或SumOfNumbers == 22或SumOfNumbers == 33:

返回

其他:

I2 = int(SumOfNumbers / 10)

F2 = SumOfNumbers - (I2 * 10)

SumOfNumbers = I2 + F2


我想要做的事情就是如果SumOfNumbers相当于上面的任何一个,我想要它

停止从那里回来。在VB中,我使用了一个GOTO语句,它在

Python中不存在。在我的生活中,我不能想出如何替换我正在使用的GOTO功能。我已经尝试了很多选项,比如,继续,休息,退货,返回SumOfNumbers,return(),而且没有

他们的工作。

这就是我想要完成的事情:


def MasterNumberRoutine(SumOfNumbers):


#这个例程决定了输入是一个主号码,如果没有,

#adds结果

如果SumOfNumbers == 11或22或33:

#go回到你来自的地方,不要继续......


I2 = int(SumOfNumbers / 10)

F2 = SumOfNumbers - (I2 * 10)

SumOfNumbers = I2 + F2


如果SumOfNumbers = 10:

#如果SumOfNumbers = 10,则设置它'的价值为1

SumOfNumbers = 1

elif SumOfNumbers> 9:

MasterNumberRoutine()

否则:

返回SumOfNumbers


这是旧例程从VB工作...我知道,这是糟糕的代码。这是多年前我写的第一个程序$



Public Sub MasterNumberRoutine()

On Error GoTo ErrorHandler


''此例程确定输入是否为主号码,如果不是,则

''添加结果


Dim I2 As Integer

Dim F2 as Integer


如果SumOfNumbers = 11或SumOfNumbers = 22或SumOfNumbers = 33那么

GoTo完成

结束如果


I2 = Int(SumOfNumbers / 10)

F2 = SumOfNumbers - (I2 * 10)

SumOfNumbers = I2 + F2


如果SumOfNumbers = 10那么

GoTo Check10

ElseIf SumOfNumbers> 9然后

MasterNumberRoutine

否则

GoTo完成

结束如果


Check10:

''如果SumOfNumbers = 10,则将其值设为1

SumOfNumbers = 1


完成:

退出_下一个:

退出Sub


ErrorHandler:

MsgBox"错误: " &安培; Err.Number _

& vbCrLf& Err.Description,_

vbOKOnly,意外错误

调用ErrLogger(MasterNumberRoutine)

恢复Exit_Next


结束子


谢谢,杰夫

解决方案



" Jeff Wagner" < JW ***** @ hotmail.com>在消息中写道

新闻:oa ******************************** @ 4ax.com ...

我正在尝试重写用VB编写的旧程序。我不能想出
如何做到以下几点:
如果SumOfNumbers == 11或SumOfNumbers == 22或SumOfNumbers == 33:
返回
否则:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

我想要做的事情是SumOfNumbers相当于上面的任何
,我希望它停止并从那里返回。在VB中,我使用了GOTO语句
,这在Python中并不存在。在我的生活中,我不能想出如何替换我正在使用的GOTO
函数。我尝试了很多选项,即继续,中断,返回,返回
SumOfNumbers,return(),但没有一个工作。




抛出异常。这将使调用例程

有机会做出不同的事情。


我不确定周围的代码是什么所以我是

假设它是内联的。


尝试:

如果SumOfNumbers == 11或SumOfNumbers == 22或者SumOfNumbers == 33:

提高SomeException

else:

I2 = int(SumOfNumbers / 10)

F2 = SumOfNumbers - (I2 * 10)

SumOfNumbers = I2 + F2

除外:

#wwina需要在这里做什么

另外,请在缩进时使用空格。一些新闻阅读器

在使用标签时根本不会缩进。为了清晰起见,我已将上面的

示例重新缩进。


顺便说一句,有可能更简单的方法来处理

命理学...


尝试这样的事情:(未经测试)


如果SumOfNumbers不在(11,22,33):

十,单位= divmod(SumOfNumbers,10)

SumOfNumbers =十位+单位


John Roth


2003年11月26日星期三21:30:16 -0500,John Roth <是ne ******** @ jhrothjr.com> wrotf:


Jeff Wagner < JW ***** @ hotmail.com>在消息中写道
新闻:oa ******************************** @ 4ax.com .. < blockquote class =post_quotes>我正在尝试重写用VB编写的旧程序。我无法弄清楚


如何执行以下操作:


如果SumOfNumbers == 11或SumOfNumbers == 22或SumOfNumbers == 33:
返回
否则:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2


,我希望它

停止并从它返回。在VB中,我使用了

Python中不存在的GOTO语句


。我不能,为了我的生活,弄清楚如何替换我正在使用的GOTO


功能。我

已经尝试了很多选项,即,继续,中断,返回,返回


SumOfNumbers,return(),并且没有

他们工作。



抛出异常。这将使调用程序有机会再做一些不同的事情。

我不确定周围的代码是什么,所以我要去承担它是内联的。

尝试:
如果SumOfNumbers == 11或SumOfNumbers == 22或SumOfNumbers == 33:
引发SomeException
否则:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2
除了:
#wake你需要在这里做什么

另外,请在缩进时使用空格。有些新闻阅读器在使用标签时根本不会缩进。为了清楚起见,我已经重新缩进了上面的
示例。

顺便说一句,有可能更简单的方法来处理数字命理......
尝试这样的事情:(未经测试)

如果SumOfNumbers不在(11,22,33):
十,unit = divmod(SumOfNumbers,10)
SumOfNumbers = tens +单位

John Roth




太棒了,非常感谢!

Jeff


Jeff Wagner< JW ***** @ hotmail.com>写在

新闻:oa ******************************** @ 4ax.com:

这就是我要完成的事情:

def MasterNumberRoutine(SumOfNumbers):

#此例程确定是否输入是一个主号码,如果不是,如果没有,则#adds结果
如果SumOfNumbers == 11或22或33:


奇怪的是,没有回复我已经看到了明显的问题,

这就是上面一行测试三个条件SumOfNumbers == 11,

" 22"和" 33" ;。如果这三个中的任何一个都是真的,那么整个表达式都是正确的,

并且22和33都是始终为真的值。


你想写点东西喜欢:


如果SumOfNumbers == 11或SumOfNumbers == 22或SumOfNumbers == 33:

返回SumOfNumbers


或者,你可能会对快活者更开心:


如果SumOfNumbers在(11,22,33):

返回SumOfNumbers #go回到哪里你来自,不要继续...

I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2
这些行试图将整数除以10,然后提取

余数。你可以使用类似的东西:


I2,F2 = SumOfNumbers // 10,SumOfNumbers%10


或者,你可以在同时:


I2,F2 = divmod(SumOfNumbers,10)


这些变量名称I2和F2完全有意义或描述性<如果SumOfNumbers = 10:
如果SumOfNumbers = 10,则将其值设置为1
SumOfNumbers = 1


在我看来,这个测试完全是多余的。如果你删除它,

,结果是10,那么递归调用会将它转换为1

无论如何。

elif SumOfNumbers> 9:
MasterNumberRoutine()
否则:
返回SumOfNumbers



递归可能看起来很酷,但在很多情况下,它可以更清晰/>
重写整个思考作为迭代解决方案:

def MasterNumberSolution(valueToSum):

而valueToSum> 9:

如果valueToSum in(11,22,33):

返回valueToSum

商,余数= divmod(valueToSum,10)

valueToSum =商+余额

返回值话说

-

Duncan Booth du **** @ rcp.co.uk

int month(char * p){return(124864 /((p [0] + p [1] -p [2]& 0x1f)+1)%12)[" \\\\\\\\\\\\\\\\\\\\\\\\ \\!\\\ xb \1 \ x9 \ xa \\\\\\ 4"];} //谁说我的代码模糊不清?


I am trying to rewrite an old program written in VB. I can''t figure out how to do the following:

if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
return
else:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

What I want this thing to do is if the SumOfNumbers is equivalent to any of the above, I want it to
stop and return from whence it came. In VB, I had used a GOTO statement which doesn''t exist in
Python. I can not, for the life of me, figure out how to replace the GOTO function I was using. I
have tried many options, i.e., continue, break, return, return SumOfNumbers, return(), and none of
them work.

This is what I am trying to accomplish:

def MasterNumberRoutine(SumOfNumbers):

#This routine determines if the input is a master number and, if not,
#adds the resultant
if SumOfNumbers == 11 or 22 or 33:
#go back to where you came from, don''t continue ...

I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

if SumOfNumbers = 10:
#If SumOfNumbers = 10, then set it''s value to 1
SumOfNumbers = 1
elif SumOfNumbers > 9:
MasterNumberRoutine()
else:
return SumOfNumbers

This is the old routine from VB which worked ... I know, it''s crappy code. It''s the first program I
wrote many years ago.

Public Sub MasterNumberRoutine()
On Error GoTo ErrorHandler

''This routine determines if the input is a master number and, if not,
''adds the resultant

Dim I2 As Integer
Dim F2 As Integer

If SumOfNumbers = 11 Or SumOfNumbers = 22 Or SumOfNumbers = 33 Then
GoTo Done
End If

I2 = Int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

If SumOfNumbers = 10 Then
GoTo Check10
ElseIf SumOfNumbers > 9 Then
MasterNumberRoutine
Else
GoTo Done
End If

Check10:
''If SumOfNumbers = 10, then set it''s value to 1
SumOfNumbers = 1

Done:
Exit_Next:
Exit Sub

ErrorHandler:
MsgBox "Error: " & Err.Number _
& vbCrLf & Err.Description, _
vbOKOnly, "Unexpected Error"
Call ErrLogger("MasterNumberRoutine")
Resume Exit_Next

End Sub

Thanks, Jeff

解决方案


"Jeff Wagner" <JW*****@hotmail.com> wrote in message
news:oa********************************@4ax.com...

I am trying to rewrite an old program written in VB. I can''t figure out how to do the following:
if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
return
else:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

What I want this thing to do is if the SumOfNumbers is equivalent to any of the above, I want it to stop and return from whence it came. In VB, I had used a GOTO statement which doesn''t exist in Python. I can not, for the life of me, figure out how to replace the GOTO function I was using. I have tried many options, i.e., continue, break, return, return SumOfNumbers, return(), and none of them work.



Throw an exception. That will give the calling routine
the opportunity to then do something different.

I''m not sure what the surrounding code is so I''m going
to assume that it''s inline.

try:
if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
raise SomeException
else:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2
except:
#whatever you need to do here
Also, please use spaces when indenting. Some newsreaders
don''t indent at all when you use tabs. I''ve reindented your
example above for clarity.

By the way, there are probably easier ways to deal with
numerology...

Try something like this: (untested)

if SumOfNumbers not in (11, 22, 33):
tens, units = divmod(SumOfNumbers, 10)
SumOfNumbers = tens + units

John Roth


On Wed, 26 Nov 2003 21:30:16 -0500, "John Roth" <ne********@jhrothjr.com> wrotf:


"Jeff Wagner" <JW*****@hotmail.com> wrote in message
news:oa********************************@4ax.com.. .

I am trying to rewrite an old program written in VB. I can''t figure out


how to do the following:


if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
return
else:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2

What I want this thing to do is if the SumOfNumbers is equivalent to any


of the above, I want it to

stop and return from whence it came. In VB, I had used a GOTO statement


which doesn''t exist in

Python. I can not, for the life of me, figure out how to replace the GOTO


function I was using. I

have tried many options, i.e., continue, break, return, return


SumOfNumbers, return(), and none of

them work.



Throw an exception. That will give the calling routine
the opportunity to then do something different.

I''m not sure what the surrounding code is so I''m going
to assume that it''s inline.

try:
if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:
raise SomeException
else:
I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2
except:
#whatever you need to do here
Also, please use spaces when indenting. Some newsreaders
don''t indent at all when you use tabs. I''ve reindented your
example above for clarity.

By the way, there are probably easier ways to deal with
numerology...

Try something like this: (untested)

if SumOfNumbers not in (11, 22, 33):
tens, units = divmod(SumOfNumbers, 10)
SumOfNumbers = tens + units

John Roth



Awesome, thanks a lot!
Jeff


Jeff Wagner <JW*****@hotmail.com> wrote in
news:oa********************************@4ax.com:

This is what I am trying to accomplish:

def MasterNumberRoutine(SumOfNumbers):

#This routine determines if the input is a master number and,
if not, #adds the resultant
if SumOfNumbers == 11 or 22 or 33:
Strangely, none of the replies I have seen mentioned the obvious problem,
which is that the line above tests the three conditions "SumOfNumbers==11",
"22", and "33". If any of these three is true the whole expression is true,
and both 22 and 33 are values which are always true.

You want to write something like:

if SumOfNumbers == 11 or SumOfNumbers==22 or SumOfNumbers==33:
return SumOfNumbers

Or, you might be happier with the snappier:

if SumOfNumbers in (11,22,33):
return SumOfNumbers #go back to where you came from, don''t continue ...

I2 = int(SumOfNumbers / 10)
F2 = SumOfNumbers - (I2 * 10)
SumOfNumbers = I2 + F2 These lines are trying to do integer division by 10 and then extract the
remainder. You could use something like:

I2, F2 = SumOfNumbers//10, SumOfNumbers%10

or, you could do both operations at the same time:

I2, F2 = divmod(SumOfNumbers, 10)

Those variable names I2 and F2 are exactly meaningful or descriptive
either.

if SumOfNumbers = 10:
#If SumOfNumbers = 10, then set it''s value to 1
SumOfNumbers = 1
It seems to me that this test is completely superfluous. If you removed it,
and the result was 10, then the recursive call would convert it to 1
anyway.
elif SumOfNumbers > 9:
MasterNumberRoutine()
else:
return SumOfNumbers


Recursion may look cool, but for many situations, it can be clearer to
rewrite the whole think as an iterative solution:

def MasterNumberSolution(valueToSum):
while valueToSum > 9:
if valueToSum in (11,22,33):
return valueToSum
quotient, remainder = divmod(valueToSum, 10)
valueToSum = quotient + remainder
return valueToSum
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


这篇关于如果别的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆