尝试这样的事情:(未经测试) 如果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,则将其值设置为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:returnelse: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 tostop and return from whence it came. In VB, I had used a GOTO statement which doesn''t exist inPython. I can not, for the life of me, figure out how to replace the GOTO function I was using. Ihave tried many options, i.e., continue, break, return, return SumOfNumbers, return(), and none ofthem 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 resultantif 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 1SumOfNumbers = 1elif 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 Iwrote 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 IntegerDim F2 As Integer If SumOfNumbers = 11 Or SumOfNumbers = 22 Or SumOfNumbers = 33 ThenGoTo DoneEnd If I2 = Int(SumOfNumbers / 10)F2 = SumOfNumbers - (I2 * 10)SumOfNumbers = I2 + F2 If SumOfNumbers = 10 ThenGoTo Check10ElseIf SumOfNumbers > 9 ThenMasterNumberRoutineElseGoTo DoneEnd If Check10:''If SumOfNumbers = 10, then set it''s value to 1SumOfNumbers = 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 messagenews:oa********************************@4ax.com... I am trying to rewrite an old program written in VB. I can''t figure outhow 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 anyof the above, I want it to stop and return from whence it came. In VB, I had used a GOTO statementwhich doesn''t exist in Python. I can not, for the life of me, figure out how to replace the GOTOfunction I was using. I have tried many options, i.e., continue, break, return, returnSumOfNumbers, return(), and none of them work. Throw an exception. That will give the calling routinethe opportunity to then do something different. I''m not sure what the surrounding code is so I''m goingto assume that it''s inline. try:if SumOfNumbers == 11 or SumOfNumbers == 22 or SumOfNumbers == 33:raise SomeExceptionelse:I2 = int(SumOfNumbers / 10)F2 = SumOfNumbers - (I2 * 10)SumOfNumbers = I2 + F2except:#whatever you need to do hereAlso, please use spaces when indenting. Some newsreadersdon''t indent at all when you use tabs. I''ve reindented yourexample above for clarity. By the way, there are probably easier ways to deal withnumerology... Try something like this: (untested) if SumOfNumbers not in (11, 22, 33):tens, units = divmod(SumOfNumbers, 10)SumOfNumbers = tens + units John RothOn Wed, 26 Nov 2003 21:30:16 -0500, "John Roth" <ne********@jhrothjr.com> wrotf: "Jeff Wagner" <JW*****@hotmail.com> wrote in messagenews:oa********************************@4ax.com.. . I am trying to rewrite an old program written in VB. I can''t figure outhow 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 anyof the above, I want it to stop and return from whence it came. In VB, I had used a GOTO statementwhich doesn''t exist in Python. I can not, for the life of me, figure out how to replace the GOTOfunction I was using. I have tried many options, i.e., continue, break, return, returnSumOfNumbers, return(), and none of them work.Throw an exception. That will give the calling routinethe opportunity to then do something different.I''m not sure what the surrounding code is so I''m goingto 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 + F2except: #whatever you need to do hereAlso, please use spaces when indenting. Some newsreadersdon''t indent at all when you use tabs. I''ve reindented yourexample above for clarity.By the way, there are probably easier ways to deal withnumerology...Try something like this: (untested)if SumOfNumbers not in (11, 22, 33): tens, units = divmod(SumOfNumbers, 10) SumOfNumbers = tens + unitsJohn Roth Awesome, thanks a lot!JeffJeff Wagner <JW*****@hotmail.com> wrote innews: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 + F2These lines are trying to do integer division by 10 and then extract theremainder. 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 descriptiveeither. if SumOfNumbers = 10: #If SumOfNumbers = 10, then set it''s value to 1 SumOfNumbers = 1It 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 1anyway. elif SumOfNumbers > 9: MasterNumberRoutine() else: return SumOfNumbersRecursion may look cool, but for many situations, it can be clearer torewrite the whole think as an iterative solution: def MasterNumberSolution(valueToSum):while valueToSum > 9:if valueToSum in (11,22,33):return valueToSumquotient, remainder = divmod(valueToSum, 10)valueToSum = quotient + remainderreturn valueToSum--Duncan Booth du****@rcp.co.ukint 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? 这篇关于如果别的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 18:37