Python代码没有给出任何结果

Python代码没有给出任何结果,python,Python,我是python新手,试图编写程序来查找amstrong和module。但我在寻找阿姆斯特朗的时候有问题,它不会结束状态,挂在中间。然而,模数工作良好。它不会抛出任何错误。这是我的密码: try: def check(s): if(s==1): print 'enter the no' v=[] s=int(raw_input()) print s

我是python新手,试图编写程序来查找amstrong和module。但我在寻找阿姆斯特朗的时候有问题,它不会结束状态,挂在中间。然而,模数工作良好。它不会抛出任何错误。这是我的密码:

     try:
       def check(s):
         if(s==1):

           print 'enter the no'
           v=[]
           s=int(raw_input())

           print s
           for x in str(s):
              print x
              v.append(x)
           print v
           x=len(v)
           i=0
           y1=0
           print v[0]
           while(i<x):

             y=int(v[i])

             y=y**3
             y1=y1+y

           print y1
           if(y1==s):
             print "given no",s,"is amstrong no"
           else:
            print "given no",s,"is not a amstrong no"
         elif(s==2):


          print 'enter the 1st no'

          s=int(raw_input())
          print 'enter the 2nd no'

          s1=int(raw_input())
          ans=s%s1
          print 'modulus ans is',ans
      finally:
        print "bye bye"


   try:

    print "1.amstrong 2.modulus"
    x=int(raw_input())
    if(x>=1 and x<=2):

      check(x)
  finally:
    print 'bye bye'

>请帮助我。

< P>挂号在中间的原因是你进入while循环,而Ei

中间挂的原因是你进入while循环,而Ei

你可以用下面的函数来检查一个数字是否是3位数的AsStand。 然而,这仅限于3位数字,但正确使用循环也可以处理更多的数字。几乎和你做的一样。但请始终记住递增或递减循环计数器,以防止无限循环。否则循环不会到达结束状态,挂在中间。< /P>

 def Ammstrong(s):

     n1=s/100
     #n1 contains the 1st digit of number, s.

     n2=s%100
     n2=n2/10
     #n2 contains the 2nd digit of number, s.

     n3=s%10
     #n3 contains the 3rd digit of number, s.

     number=(n1**3)+(n2**3)+(n3**3)
     if number==s:
     print number,"is an Ammstron Number" 
     #return number
     else: 
     print number,"is not an Ammstron Number"
     #return number
num=input(Enter a number: ")
Ammstrong(num)
#use this if return is uesd:
#number=Ammstrong(num)
此功能将打印答案。因此,不需要使用主功能中的打印功能来打印答案

如果您希望执行进一步的计算,请使用“返回”。这在使用注释编码的文档中进行了说明。但是,一旦执行返回执行,执行语句的程序就会终止。这意味着该函数不会终止主程序。因此,在打印功能之后使用返回功能。 如果使用返回函数,则必须将返回值存储在变量中

还有一件事: 在启动变量x之前,您已经使用了它。 并且,不要使用s==2语句,而是使用s=0. != 符号不相等吗

祝你学习python好运。这是一门非常有趣的语言


将逐步向您显示代码的执行情况。

您可以使用下面编码的函数检查数字是否为3位ammstrong。 然而,这仅限于3位数字,但正确使用循环也可以处理更多的数字。几乎和你做的一样。但请始终记住递增或递减循环计数器,以防止无限循环。否则循环不会到达结束状态,挂在中间。< /P>
 def Ammstrong(s):

     n1=s/100
     #n1 contains the 1st digit of number, s.

     n2=s%100
     n2=n2/10
     #n2 contains the 2nd digit of number, s.

     n3=s%10
     #n3 contains the 3rd digit of number, s.

     number=(n1**3)+(n2**3)+(n3**3)
     if number==s:
     print number,"is an Ammstron Number" 
     #return number
     else: 
     print number,"is not an Ammstron Number"
     #return number
num=input(Enter a number: ")
Ammstrong(num)
#use this if return is uesd:
#number=Ammstrong(num)
此功能将打印答案。因此,不需要使用主功能中的打印功能来打印答案

如果您希望执行进一步的计算,请使用“返回”。这在使用注释编码的文档中进行了说明。但是,一旦执行返回执行,执行语句的程序就会终止。这意味着该函数不会终止主程序。因此,在打印功能之后使用返回功能。 如果使用返回函数,则必须将返回值存储在变量中

还有一件事: 在启动变量x之前,您已经使用了它。 并且,不要使用s==2语句,而是使用s=0. != 符号不相等吗

祝你学习python好运。这是一门非常有趣的语言


将逐步向您展示代码的执行情况。

您的除块在哪里?我使用的是try and finally,这是给定问题吗?您的除块在哪里?我使用的是try and finally,这是给定问题吗?@kallis祝您好运。在Python中,我们使用的是except not catch。@kallis祝您好运。在Python中,我们使用的是except not catch。