Also, the last two examples both have an empty line. There is one empty space on each side of the equal sign. Notice that when we define a variable, the pattern is name = value. Look back at the code examples on the previous page. Anything inside two if statements should start eight spaces from the wall.Anything inside ONE if statement should start four spaces from the wall.Anything not in an if statement should start at the “wall.”. # Otherwise, the robot will choose scissors.Ĭonsistent indentation makes code easier to read and can protect it from an IndentationError.Ĭorrect the bad example code following these rules: # If user's number is odd and bigger than 5, the robot will choose paper. # If user's number is even and bigger than 5, the robot will choose rock. Python will ignore everything on the line after it, so it won’t mess up your code.Īdd comments to the three main if statements in the Rock, Paper, Scissors code to explain what each one does like so: notice the space! To add a comment in Python, use the # symbol. You can name a variable whatever you want, but it’s best to name it something that tells us what it does.Īdding comments to our code can make it easier to understand. There are two variables defined at the top: thing1 and thing2. This code works, but it’s hard to understand for several reasons. This lesson will explore the basics of how we can make sure our code is easy to read and understand for ourselves and for others. Is your handwriting hard to read? Have you ever tried to read something you wrote, but couldn’t because it was so messy? Although we write code by typing, something similar can happen if we aren’t careful.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |