From 9b8be66813b0dd1653f4dc9f8eb78854a6a37077 Mon Sep 17 00:00:00 2001 From: Alfie Bowman Date: Fri, 16 Oct 2015 20:10:58 +0100 Subject: [PATCH] style code correctly Code styled with fenced blocks and syntax highlighting. --- Day-1/README.md | 60 +++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/Day-1/README.md b/Day-1/README.md index 627ab12..ea03a78 100644 --- a/Day-1/README.md +++ b/Day-1/README.md @@ -11,7 +11,9 @@ Open Python IDLE. This opens the output window - we will write our code in a new In the new window, type this to print something: - print "hello world" +```python +print "hello world" +``` Save the project as a .py extension (you can hit CTRL/CMD + S to save). Hit F5 to run your program. @@ -19,48 +21,56 @@ Create your first variable and print it. In computer language, a variable is wh Below that is a `print` statement that shows you how to print a variable. - print "hello world" - name = "Brian" - print "my name is", name +```python +print "hello world" +name = "Brian" +print "my name is", name +``` The next task is to ask the user for something. That's what `raw_input` does. We can have the user type in something and store it in a variable. - print "hello world" - name = "Brian" - print "my name is", name - - your_name = raw_input("What is your name?") +```python +print "hello world" +name = "Brian" +print "my name is", name +your_name = raw_input("What is your name?") +``` + If you run this, you'll see that there's no space between the `?` and the user's input. We can fix this by adding a space in. When you use `raw_input`, it stores what the user typed in in a variable. We can print the info the user typed in: - print "hello world" - name = "Brian" - print "my name is", name - - your_name = raw_input("What is your name? ") - print "your name is", your_name +```python +print "hello world" +name = "Brian" +print "my name is", name +your_name = raw_input("What is your name? ") +print "your name is", your_name +``` + You can use `raw_input` to ask the user for strings - things like names and sentences. If you use `input`, Python will try to change what the user typed into a number. - print "hello world" - name = "Brian" - print "my name is", name +```python +print "hello world" +name = "Brian" +print "my name is", name - your_name = raw_input("What is your name? ") - print "your name is", your_name - your_age = input("How old are you? ") - print "in a year you will be", your_age+1, "years old" +your_name = raw_input("What is your name? ") +print "your name is", your_name +your_age = input("How old are you? ") +print "in a year you will be", your_age+1, "years old" +``` If you try to type "nine" when it asks for the age, it won't work. Python doesn't know what "nine" is, it only know the digit "9". There are two types of variables we'll be using today: strings (things like words, sentences), and integers (whole numbers). Python thinks "nine" is a string, and can't change it to a number. -**Bonus challenge: In the example above, we added 1 to your age and told you how old you will be in a year. Can you set a new variable years_to_100 that will calculate how many years until you turn 100? Then print out the variable with a message. +**Bonus challenge**: In the example above, we added 1 to your age and told you how old you will be in a year. Can you set a new variable years_to_100 that will calculate how many years until you turn 100? Then print out the variable with a message. ##Reading and Altering Code - Number Guess Game @@ -112,7 +122,9 @@ We'll be making a lot more changes to this program to get it to do what we want. You could use a for loop that repeats code a specific number of times. This line will repeat the indented code 10 times: - for num in range(0,10): + ```python + for num in range(0,10): + ``` You could also use a while loop and count yourself. See how the number guess game counted the number of tries.