most recent assessment

advertisement
Type in the following in python
Then create three further variables with appropriate strings
This is an assessment
Variables –recap-python
name = input(“What is your name? ”)
print(“Hello,”,name)
Assessment point 2-python arithmetic
x = int(input(“Enter a number between 1 and
100: “))
y = int(input(“Enter another number between 1
and 100: “))
print(“X + Y =”,x+y)
You should now create a arithmetic program to
Multiply
Divide
MOD
You have 10 minutes to do this
example
hole1 = int(input(“Enter a number between 1 and 100: “))
Hole 2 = int(input(“Enter another number between 1 and
100: “))
print(“you have the following score =”,hole1+hole2 /4)
Now extend the program by working out oif the score reached by players is over par
Notice I have divided the end result using the / symbol to give an average score per hole
Par= amount of shots allowed for the golf hole
Nb:Remember to use par==4 for example
Using if then else
par = int(input(“what was your golf score ? “))
if par < 2
print(“That seems a great score you have met par for course. Well
done!”)
else:
print(“You need more practice .”)
par = int(input(“what was your golf score ? “))
if par > 2:
print(“That seems over par -bad luck!”)
else:
print(“You have a wonderful score .”)
Using the inputs from challenge 2, the “par” for the
course is 11. The system should tell the user if their
score was “over par” or “under par
If statements-assessment point 3
Your task now is to combine a comparative operator with an input. Computers often use if
statements to determine what decision to make.
Task 1. Let’s write a basic if statement using a variable first.
age = int(input("What is your age?"))
if age < 16:
print("you are young!")
else:
print("your are getting old!")
**Rewrite this if statement to ask how many marbles I have. (Notice how the code int is in
front of input because the data being entered is a number.)
If the person has less than 6 marbles, it should reply “I have more than you”
If the answer is else, it should reply, “Wow that’s a big collection of marbles!!
More If Statements
If statements can check for a list of things instead of just 1. For
example:
temp = int(input("What's the temperature in celcius?"))
if temp < 8:
print("Brr, its a cold day!!!")
elif temp >= 8 and temp < 15:
Can you see that the word if is only used
print("I'ts a mild day")
once?
elif temp >= 15 and temp < 21: Every other time to check if something is
True, Python uses elif.
print("Its a warm day")
elif temp >= 21:
print("It's a hot day")
else:
print("sorry, you must enter a number")
Even more if statements
- IF ... ELIF ... ELSE
Sometimes there are more than two options. I could walk OR cycle OR
get the bus OR get a lift. As well as IF and
ELSE, we can stick an ‘ELSE IF’ (or ELIF) in the middle:
x = int(input(“How many hours a day do you play computer games? “)
if x < 2:
print(“That seems a fairly healthy balance. Well done!”)
elif x < 4:
print(“You’re probably good enough by now with all that practice.”)
else:
print(“Put the controller down and get some fresh air once in a
while!”)
Even even more if statements
IF ... ELIF ... ELIF ... ELIF ... ELSE
You can include an unlimited number of ELIFs if you need to. Try the following:
menu = "What would you like:\n\
1. A complement?\n\
2. An insult?\n\
3. A proverb?\n\
4. An idiom?\n\
9. Quit\n"
x = int(input(menu))
if x == 1:
print("You look lovely today!")
elif x == 2:
print("You smell funny.")
elif x == 3:
print("Two wrongs don't make a right. But three lefts do...")
elif x == 4:
print("The pen is mightier than the sword.")
elif x == 9:
print("Goodbye!!!")
There are a couple of
important bits here:
• You can put a line break in
your string by using “\n”.
• You can continue a line of
code by putting a “\” at the
end.
• If you are testing for
equality, use a double equals
(is 3x2 == 6?)
If statements -Task 1
Write a programme to check if someone’s
name is John, George, Ringo or Paul. If one
of those names is True, Python should say
“Hey that’s the name of a Beatle!”.
If not, Python should say “That’s a nice
name”.
Write your answer in the next box
Task 2 if statements
Write a programme to comment on how interesting a football
match was.
❏ If no goals were scored, Python
should say “the game was a bore
draw!”
❏ 12 goals: “Not the most interesting
game”
❏ 35 goals: “It was a very interesting
game”
❏ 6+ goals: “The football match was an
unmissable game!”
Assessment 3-if statements
3 – IF
Using the inputs from challenge 2, the “par” for
Statements the course is 11. The system should tell the user if
their score was “over par” or “under par”
4 – Else IF
Again using the inputs from question 2, give the
user some feedback. If their score was:
 Less than 11 then say “Well done, you should go
pro”
 Equal to 11 then say “Well done, you made par”
 More than 11 then say “More practice needed”
Looping-while loops
Computers can be programmed to continue repeating code while a condition is
True.
Look at the example below for an idea
name = ""
while name != "Batman":
print("Somebody call Batman!")
print("Are you him? ")
name = input("What's your name?")
As you can see in the example in the left, the code will keeping looping and
asking the same question while the answer is not Batman. Notice the nesting of
statements underneath the while? It is the indented code that will keep
looping
Task 1
Write a while loop to ask someone what the
best football team in the world is. Repeat the
question until they say the correct team.
Harder loops!
Now we will try to write one more while loop this time using a condition.
Again this task is a lot harder. Look at the example below and try to write
your own.
secret_number = 6
guesses = 3
while guesses > 0:
print("Can you guess what number I am thinking?)
guess = int(input("write a number "))
In this code, we also learn of the break
if guess == secret_number:
statement.
print("Well done, you guessed correctly!")
break
This will tell the PC to skip to the next
line of code that is not in the current
else:
indented nesting.
guesses = guesses 1
if guesses == 0:
print("You have no guesses left!")
Loops harder still
The code below shows an example of how
to add a list of numbers together:
count = 5
number = 0
print("this programme will work out the
total of a list of numbers")
while count > 0:
number = number + int(input(print("Please
enter a number :")))
count = count 1
print(number)
Loop assessment 1
5 – For
Loops
Write a for loop that will print the following sentence 20 times:
“I must listen to my IT teachers at all times, because they are very
intelligent”
Loop assessment 2
Copy the code for challenge 5, but this time ask the user to input how many
times they would like that sentence printed. The for loop should then print
only than number of times the sentence:
“I must listen to my IT teachers at all times, because they are very
intelligent”
While loops assessment
• Ask the user if they would like some advice
(Yes or No). Write a while loop that will
continue asking the user if they want advice
until they answer “Yes”. At which point the
following must be printed to the screen:
• “Always list to your IT teachers
• Note that the program should accept both
“Yes” or “yes” for the answer
While loops extension task
A game of cricket has 6 balls per over. Write a
while loop that asks for the user to input the
score of each bowl and at the end will tell you
the score for the over. For example (0,2,0,0,4,6
would total to 26 for the over)
And now for pygame!
• See pygame tutorial
Download