Pages


Wednesday, 12 June 2019

(Post 9/Year 1 Week 1)Learning python part 9:For Loop

In this post,we will cover loop

hello Let's do a very simple For loop

Code

##for looper in the list containing 1,2,3,4,5 print hello for looper in [1, 2, 3, 4, 5]: print("hello")
Output



Let try again, by changing the looper

Code


##it need not be looper ,it can be anything eg I or thing ##for thing in the list containing 1,2,3,4,5 print hello for thing in [1, 2, 3, 4, 5]: print("hello")
Output


Using a counting loop

Now let’s do something a bit more useful with loops. Let’s print a multiplication table. It
only takes a small change to our program. Here’s the new version.

##for looper in the list 1,2,3,4,5 print looper,"times 8",looper multiply by 8 for looper in [1, 2, 3, 4, 5]: print(looper, "times 8 =", looper * 8)
Output



In the previous example, we only looped 5 times:
But what if we wanted the loop to run 100 times, or 1,000 times? That would be a lot
of typing!

Luckily, there’s a shortcut. The range() function lets you enter just the starting and ending
values, and it creates all the values in between for you. range() creates a list containing a
range of numbers.

The next listing uses the range() function in the multiplication table example.

Code

##for looper in the range of 1 to 5,print looper,"times 8=',looper *8 for looper in range (1, 5): print(looper, "times 8 =", looper * 8)

Output



It’s almost the same as the first result … except that it missed the last loop! Why? Because range (1, 5) gives us the list [1, 2, 3, 4].

Code
##print(range 1 to 5) print range(1, 5)

Output


Let's  now modify the loop to print ten times

Code

##for looper in range 1 to 11,print looper,"times 8",looper *8 for looper in range(1, 11): print looper, "times 8 =", looper * 8
Output



Remenber ,i say that looper can be change with anything e.g such as I,Now let change the looper with i
##for I in range from 1 to 5,print I,"times 8=,i*8
For i in range(1, 5):
 print(i, "times 8 =", i * 8)
Output


Now let's try a range shortcut

Code
##for I in range (5),print i
for i in range(5):
   print(i)
Output


This is the same as the below code

Code


##for i in range 0 to 4,print i
for i in range(0, 5):
    print(i)
Output


Sunday, 10 February 2019

(Post 8/Year 1 Week 1)Learning python part 8:IF ELSE ELIF decision

Perhaps,you are still confuse with if else decision from the previous post on number guessing game?don't fret,this post is here to save your day Let's try out some simple code below

IF ELSE

Code

##set 5 + 3 to correctAnswer 
correctAnswer = 5 + 3

##set 35 to temperature 
temperature = 35

##set name to bill
name = "Bill"

## check if correctAnswer is equal to 8,if correctanswer print out 8
##to test things if they are equal python use == sign
##note there is an indentation or what you call space after the if statement
##there is an : at the end of an if,else and elif statement
if correctAnswer ==8:
   print("correctanswer")
## else if the correctAnswer is not equal to 8 ,print "wrong answer"
else:
  print("wronganswer")


##check if temperature is equal to 40,if true print "correct answer" 
if temperature == 40:
    print("correct answer")
##else if temperature is not equal to 40,print "wrong answer"
else:
    print("wrong answer")


##check if name is equal to Bill,if true print "correct answer" 
if name == "Bill":
   print("correct answer")
##else if name is not equal to bill print "wrong answer"
else:
   print("wrong answer")
Output

Exactly like it said

Comparing Numbers

Now let's try to compare some numbers

##set num1 as the integer 4
num1= 4

##set num 2 as the integer 10
num2=10

##if num 1 is less than num 2 print out num1 is less than num2 
if num1 < num2:
 print(num1, "is less than", num2)
##if num 1 is more than num 2 ,print out num 1 is more than num 2 
if num1 > num2:
 print(num1, "is greater than", num2)
##if num 1 is equal to num 2 ,print out num1 is equal to num 2 

if num1 == num2:
 print(num1, "is equal to", num2)
##if num1 is not equal to num2 
if num1 != num2:
 print(num1, "is not equal to", num2)

Output


Let's try again by replacing the num with your own input

#all your input are string hence they need to be convert to int
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))


if num1 < num2:
 print(num1, "is less than", num2 )
if num1 > num2:
 print(num1, "is greater than", num2 )
if num1 == num2:
 print(num1, "is equal to", num2 )
if num1 != num2:
 print (num1, "is not equal to", num2)
Output



IF ELSE ELIF

Now,we are going to try something diff,if elif and else.

Elif allow you to modify the condition

##set answer as inputanswer=int (input("Enter the mark you got on your test"))

##if answer is greater or equal to 10,you got at least 10
if answer >= 10:
 print("You got at least 10!")

##else if answer is greater or equal to 5,print you got at least 5 
elif answer >= 5:
 print ("You got at least 5!")

##else if answer is greater or equal to 3,print you got at least 3 
elif answer >= 3:
 print("You got at least 3!")

##else if it is not any above,print you got less than 3 
else:
 print("You got less than 3.")
Output


Testing for more than one condition

What if you want to test for more than one thing? Let’s say we made a game that was for  eight-year-olds and up, and we want to make sure the player is in at least third grade. There  are two conditions to meet. Here is one way we could test for both conditions:

Code


##input to enter your age
age = float(input("Enter your age: "))

##input to enter your grade 
grade = int(input("Enter your grade: "))

##if age is more than or equal to 8 
if age >= 8:
##if grade is more than or equal to 3,remember to indent 
 if grade >= 3:
##print "you can play this game " 
 print "You can play this game."
##else 
else:
##print "sorry,you can't  play the game" 
 print "Sorry, you can't play the game."



Output

Using and

That last example will work fine. But there is a shorter way to do the same thing. You can  combine conditions like this:

##input to enter your age
age = float(input("Enter your age: "))

##input to enter your grade 
grade = int(input("Enter your grade: "))

##if age is more than or equal to 8 and grade is more than or equal to 3 
if age >= 8 and grade >= 3:
##print "you can play this game 
 print "You can play this game."
##else 
else:
##print "sorry,you can't  play the game 
 print "Sorry, you can't play the game.
Output

More and condition

You can put more than two conditions together with and:

Code


##input to enter your age
age = float(input("Enter your age: "))

##input to enter your grade 
grade = int(input("Enter your grade: "))

##input to enter your favourite color 
color = input("Enter your favorite color: ")

##if age is more than or equal to 8 and grade is more than or equal to 3 and color is equal to green,print" you are allowed to play this game" 
if age >= 8 and grade >= 3 and color == "green":
 print "You are allowed to play this game."

##else,print "sorry ,you can't play the game" 
else:
 print "Sorry, you can't play the game."

Output

If there are more than two conditions, all the conditions have to be true for the if
statement to be true. There are other ways of combining conditions too.

Using or

The or keyword is also used to put conditions together. If you use or, the block is executed
if any of the conditions are true

Code:
##input enter your favourite color color = input("Enter your favorite color: ") ##if color is equal to red or color is equal to blue or color is equal to green,print"you are allowed to play this game if color == "red" or color == "blue" or color == "green": print "You are allowed to play this game." else: print "Sorry, you can't play the game."

Output