Reputation: 21
I am a beginner in python . i am getting this error in python . please tell me the logic behind this error .
def sequence (n):
while n ! == 1:
print(n)
if n%2 == 0:
n = n/2
else:
n=n*3+1
please tell me the logic behind this error
Upvotes: 0
Views: 159
Reputation: 1179
# declaring a function that takes a value n as a parameter
def sequence (n):
# checking if n is not equal to 1, if yes moving into the loop
while n != 1: # n ! == 1 will give error, this is not the correct syntax to compare values in python
# printing the value
print(n)
# if n is an even number then diving n by 2 ( integer division )
if n%2 == 0:
n = n//2
# if n is not even then using the following formula
else:
n = n*3 + 1
# calling the function to execute the operation that we wrote before
sequence(10)
Gives the following output:
10
5
16
8
4
2
I recommend you to learn python by following a YouTube video first and then get along with the syntax as you go through. Best of luck!
Upvotes: 1