Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4 Computer Programming Gray , Calibri 24

Similar presentations


Presentation on theme: "Week 4 Computer Programming Gray , Calibri 24"— Presentation transcript:

1 Week 4 Computer Programming Gray 80 69 69 69, Calibri 24
Dark Red RGB , Calibri 54

2 Save the program as MathsQuiz.py Then Run it (Press F5)
You are now going to create a new program in Python. Open Python in SCRIPT mode (Click File, then New Window) Enter in the following code carefully. print("What is 2 + 2?") answer = input () answer = int(answer) if answer == 4: print("Well done") else: print("Sorry the answer was 4") Save the program as MathsQuiz.py Then Run it (Press F5)

3 if answer == 4: else: print("Well done")
…a closer look at the code You have just written your first structured program. The if statement is a very, very useful structure to know. When typing it into Python, be very careful not to forget the little colons : if answer == 4: print("Well done") else: print("Sorry the answer was 4")

4 answer = int(answer) …a closer look at the code
print("What is 2 + 2?") answer = input () answer = int(answer) This is an assignment statement just like you have been using before. However, you’ll notice that the variable ‘answer’ is surrounded by brackets and the word ‘int’. Int stands for integer. An integer is just any number without a decimal part. Examples of integers are 75, 2, 100, 55, (12.3 is NOT an integer) Therefore int(answer) converts the variable answer to an integer which is important!

5 if answer == 4: e.g. my_school = ‘Dalriada’:
…a closer look at the code if answer == 4: You’ve probably never seen this before! == A double-equal sign simply means ‘is it equal to?’. e.g. if my_school == ‘Dalriada’: reads as if my school is equal to Dalriada A single-equal sign is only used for assignment. = e.g. my_school = ‘Dalriada’: reads as The variable my_school is assigned Dalriada

6 IF the answer is equal to 4 THEN ELSE
…a closer look at the code if answer == 4: print("Well done") else: print("Sorry the answer was 4") The above section of the code is understood as follows: IF the answer is equal to 4 THEN print ‘Well Done’ ELSE print ‘Sorry the answer was 4’

7 Look at the program below. For some reason, it won’t run
Look at the program below. For some reason, it won’t run!!! Can you spot the 7 mistakes made? You have 2 mins Print('Please enter your name: ') my_name = input print(my_name ' what age are you') my_age = input() my_age = integer(my_age if my_age > 11 print('You must go to Secondary school') else print('You must go to Primary school') 0:40 0:39 0:41 0:42 0:38 0:43 0:44 0:36 0:33 0:32 0:34 0:35 0:45 0:37 0:47 0:55 0:54 0:56 0:57 0:59 0:58 0:53 0:52 0:48 0:31 0:49 0:50 0:51 0:46 0:29 0:10 0:09 0:11 0:12 0:14 0:13 0:08 0:07 0:03 End 0:04 0:05 0:06 0:15 0:16 0:25 0:24 0:26 0:27 0:28 0:23 0:22 0:18 0:17 0:19 0:20 0:21 0:30 1:00 1:41 1:40 1:42 1:43 1:45 1:44 1:39 1:38 1:33 1:01 1:34 1:35 1:37 1:36 1:46 1:47 1:56 1:55 1:57 1:58 2:00 1:59 1:54 1:53 1:49 1:48 1:50 1:51 1:52 1:31 1:32 1:10 1:09 1:11 1:12 1:14 1:13 1:08 1:07 1:03 1:02 1:04 1:05 1:06 1:30 1:15 1:25 1:16 1:26 1:27 1:29 1:28 1:23 1:24 1:22 1:17 1:19 1:20 1:21 1:18 0:02 0:01 Teacher > Click to Start Timer

8 print('Please enter your name: ') my_name = input()
Look at the program below. For some reason, it won’t run!!! Can you spot the 7 mistakes made? The Answers! print('Please enter your name: ') my_name = input() print(my_name, ' what age are you') my_age = input() my_age = int(my_age) if my_age > 11: print('You must go to Secondary school') else: print('You must go to Primary school')

9 Save as ‘Number_Guess.py’
If you can, do all these programs in SCRIPT mode (i.e. go to FILE, NEW WINDOW…then press F5 to run) Task: Guess the number between 1 & 10 Game. Create a program in python that prompts the user for a number between 1 and 10. The user’s guess, which could be assigned to a variable user_guess, should be compared to the number variable my_number which will be set by the programmer (you). If they guess the correct number, a ‘well done’ message should appear. If they guess wrong, a ‘sorry, better luck next time’ message should appear. Example… Welcome to Guess the Number game! Please enter your guess (between 1 and 10): 7 Well done! You guessed correctly! Save as ‘Number_Guess.py’


Download ppt "Week 4 Computer Programming Gray , Calibri 24"

Similar presentations


Ads by Google