Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remainder operator %
#1
Hello, just joined the forum
I am going through the pdf Automating the boring stuff with Python and I am stuck on this little program.
I don't what I am doing wrong. I did whittle the code down to show the error.
Any help is appreciated.

Oops, found my error I forgot the colon after the if statement. You don't need colons at the end of if statement in C : )

I get the error :

Traceback (most recent call last):
File "C:\Users\Joe\Documents\Python\PcOfPy.py", line 2
if number % 2 == 0
^
SyntaxError: invalid syntax


def collatz(number):
    if number % 2 == 0
        number = number //2
        print(number)
        return number
  
x= 10
collatz(x) 
Reply
#2
"if number % 2 == 0" should be "if number % 2 == 0:"
Reply
#3
hi,
you forgot the ":" at the end of the line two.
if it s not correct after, check your indentation.

hope that will be useful
Reply
#4
Yes that was it. Thank you

I am getting started with python but come from a C background.

Another thing that did not work was this.

while (collatz(x) != 1):
I had to do this to make it work
while (x != 1):
    x = collatz(x)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a remainder to int to str tester_V 6 2,879 Oct-28-2020, 06:14 PM
Last Post: tester_V
  Could not parse the remainder: '=' from '=' Saurabh 3 14,759 Feb-20-2019, 11:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020