Presentation on theme: "Variables –recap-python"— Presentation transcript:
1Variables –recap-python Type in the following in pythonThen create three further variables with appropriate stringsThis is an assessmentVariables –recap-pythonname = input(“What is your name? ”)print(“Hello,”,name)
2Assessment 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 toMultiplyDivideMODYou have 10 minutes to do this
3example 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 parNotice I have divided the end result using the / symbol to give an average score per holePar= amount of shots allowed for the golf holeNb:Remember to use par==4 for example
4Using if then elsepar = int(input(“what was your golf score ? “))if par < 2print(“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
5If 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!!
6More If StatementsIf 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: print("I'ts a mild day") elif temp >= 15 and temp < 21: print("Its a warm day") elif temp >= 21: print("It's a hot day") else: print("sorry, you must enter a number")Can you see that the word if is only usedonce?Every other time to check if something isTrue, Python uses elif.
7Even 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!”)
8Even even more if statements IF ... ELIF ... ELIF ... ELIF ... ELSEYou 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?)
9If statements -Task 1Write 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
10Task 2 if statementsWrite 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 ❏ 6+ goals: “The football match was an unmissable game!”
11Assessment 3-if statements 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”4 – Else IFAgain 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”
12Looping-while loopsComputers 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
13Task 1Write a while loop to ask someone what the best football team in the world is. Repeat the question until they say the correct team.
14Harder 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 ")) if guess == secret_number: print("Well done, you guessed correctly!") break else: guesses = guesses 1 if guesses == 0: print("You have no guesses left!")In this code, we also learn of the breakstatement.This will tell the PC to skip to the next line of code that is not in the current indented nesting.
15Loops harder stillThe 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)
16Loop assessment 15 – For LoopsWrite 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”
17Loop assessment 2Copy 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”
18While 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 teachersNote that the program should accept both “Yes” or “yes” for the answer
19While 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)