Reputation: 23
I am new to python,trying to write program for finding amstrong and modulus . But I have problem in finding amstrong no,it will not go to end state,hang in the middle. However,modulus is working fine. It will not throw any error. Here is my code:
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'
Please help me on this.
Upvotes: 1
Views: 114
Reputation: 16
You can check if a number is a 3-digit ammstrong using the function coded below. This however is restricted to only 3 digits, but using loops correctly this will work of a greater number of digits as well. Almost in the way you have done. But always remember to increment or decrement the loop counter to prevent infinite loop. Otherwise the loop "will not go to end state,hang in the middle".
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)
This function will print the answer. So, there wont be any need to use the print function in the main function to print your answer.
If you wish to perform further calculations, use 'return'. This is illustrated in the coded using comments. But, as soon as the return execution is executed, the program in which the statement is executed, terminates. Meaning the function will terminate not the main program. So, use the return function after the print function. If you do use the return function u'll have to store the returned value in a variable.
One thing more: You have made use of the variable x before initiating it. And, instead of using (s==2) statement use (s!=0). "!=" is the symbol for "not equal"
Best of luck with learning python. It is a very interesting language.
http://pythontutor.com will show you the execution of the code step by step.
Upvotes: 0
Reputation: 5059
The reason it's hanging in the middle is that you enter into the while
loop while(i<x):
, but you never change the value of either i
or x
. (You do change y
and y1
, but your conditional doesn't involve either.) The condition never ends up being false, and can't ever end up being false, so it continues to execute forever.
Note also that you're not really using try
blocks correctly. There's no point in using try
unless you're using except
to handle any exceptions. (On a different level, you shouldn't be wrapping the entirety of your code in a try
block in the first place - exceptions are useful information that make it easier to discover ways your program isn't working correctly, and ignoring them can lead to unpredictable, difficult-to-debug states.)
Last piece of advice - recognizing and fixing your problems is almost universally made easier (for both of us) by using distinct, pertinent names to your variables - it's very difficult to figure out what you're doing when every single variable is a single letter, and some are just a letter you've already used with a number appended.
Upvotes: 5