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

Tuesday, 4 December 2018

(Post 7/Year 1 Week 1)Learning python part 7:GUI(Graphic User Interface) + number guessing game

In this post,we will be using the knowledge from the previous post to create a number guessing game with GUI

It is Similar to post 3 number guessing game but with GUI

Code

##import the module random and easygui
import random, easygui


##set secret as a random number from 1 to 99 

secret = random.randint(1, 99)

##set guess to 0 
guess = 0

##set tries to 0 
tries = 0


##print out the message in the easygui msgbox 
easygui.msgbox("""AHOY! I'm the Dread Pirate Roberts, and I have a secret!
It is a number from 1 to 99. I'll give you 6 tries.""")



##while guest is not equal to secret and your tries is less than 6,gui will ask for your guess 
while guess != secret and tries < 6:
##integerbox to ask what is your guess 
 guess = easygui.integerbox("What's yer guess, matey?")

##if guess is nothing,break will indicate exit out of loop 
 if not guess:
  break
##if guess is less than secret 
 if guess < secret:

##gui will print out your input and print out is too low 
    easygui.msgbox(str(guess) + " is too low, ye scurvy dog!")



##elif guess is more than secret 

 elif guess > secret:

##gui will print out your input and print that your guess is too high 
     easygui.msgbox(str(guess) + " is too high, landlubber!")

##tries will increase by 1 
 tries = tries + 1



##if guess is equal to secret 
if guess == secret:



##gui will print that you have found his secret message 

 easygui.msgbox("Avast! Ye got it! Found my secret, ye did!")


##else 

else:

##if your tries exceeds 6 times,gui will print out the message and try you no more try 
 easygui.msgbox("No more guesses! The number was " + str(secret)) 
Output


Click ok,try 80



result,click ok


click ok,try again,60


result,click ok


click ok,try again 70


result,click ok


click ok,try again 75


result,click ok



click ok,try again 74



result,click ok



Monday, 3 December 2018

(Post 6/Year 1 Week 1)Learning python part 6:GUI(Graphic User Interface)

In this post,we are going to learn about GUI. GUI is an abbreviation for graphical user interface. In a GUI, instead of just typing text and getting text back, the user sees graphical things like windows, buttons, text boxes, and so on, and she can use the mouse to click things as well as type on the keyboard. One example of a GUI is a web browser ,there are button for you to press and text box to type in. We are going to use a module known as easygui,in a future we are going to use a module known as thinkter. EasyGui is a Python module that makes it very easy to make simple GUIs.If do have not install the python module Easygui,install it using pip install python and run it


##installing the module easygui
!pip install easygui

##or
pip install easygui
First,we are going to create a simple GUI box with "hello world" in it

Code
##import to import modules
##if error run pip install easygui import easygui ##function to create a box that has a hello world text in it easygui.msgbox("Hello World!")
Output



Click ok to close the box The above example does not show much but the next example will show more about the power of GUI We’re going to use the example of choosing your favorite flavor of ice cream to look at some different ways to get input (the ice cream flavor) from the user with EasyGui. Dialog box with multiple button Let’s make a dialog box (like a message box) with more than one button. The way to do this is with a button box (buttonbox) Code
##importing the module easygui import easygui ##a button box hold the text and the choices flavor = easygui.buttonbox("What is your favorite ice cream flavor?", choices = ['Vanilla', 'Chocolate', 'Strawberry'] ) ##the flavor is save when you click one of the the three ice cream flavor "Vanilla","Chocolate ","Strawberry" easygui.msgbox ("You picked " + flavor)

Output



i Click on chocolate and it shows...




Choice box EasyGui has something called a choice box (choicebox), which presents a list of choices. The user picks one and then clicks the OK button.

To try this, we only need to make one small change to our program: change buttonbox to choicebox


Code 

##importing the module the easygui
import easygui

##set flavor as the choices for the choices that you click 

flavor = easygui.choicebox("What is your favorite ice cream flavor?",  choices = ['Vanilla', 'Chocolate','Strawberry'] )

##print out the message after you click on the flavor 

easygui.msgbox ("You picked " + flavor)

Output



I double click chocolate



After you click a flavor and then click OK, you’ll see the same kind of message box as before. Notice that, in addition to clicking choices with the mouse, you can select a flavor with the up and down arrow keys on the keyboard. If you click Cancel, the program will end, and you’ll also see an error. That’s because the last line of 
the program is expecting some text (like Vanilla), but if you click Cancel, it doesn’t get any.



Text input

What if you want something more like input(), where the user can type in text? That way, she can enter any flavor she wants. EasyGui has something called an enter box (enterbox) to do just that.

Run the code below 

Code


##importing the module easygui
import easygui

##set flavor as whatever you want
##this is similar to input()
flavor = easygui.enterbox("What is your favorite ice cream flavor?")

##print out the message after you type inside the text box 
##text you type,e.g I type peppermint
easygui.msgbox ("You entered " + flavor)


Output



i type peppermint and click ok



Default input

Sometimes when a user is entering information, a certain answer is expected, common, or most likely to be entered. That is called a default. You might be able to save the user some typing by automatically entering the most common answer for him. Then he’d only have to type an answer if he had a different input. To put a default in an enter box, change your program to look like the code below

Code


##import the module 
import easygui

##set the flavor by whatever you type,default is vanilla if you did not type anything 
flavor = easygui.enterbox("What is your favorite ice cream flavor?", default = 'Vanilla') 

##print out the flavor that you entered or the default vanilla 
easygui.msgbox ("You entered " + flavor)

Output




Now, when you run it, “Vanilla” is already entered in the enter box. You can delete it and enter anything you want, but if your favorite flavor is vanilla, you don’t have to type anything: just click OK.



Numbers

What if you want to type numbers?

EasyGui also has something called an integer box (integerbox), which you can use for entering integers. You can set a lower and upper limit to the number that can be entered.

Let modify the above code


##importing the module easygui
import easygui

##set the default for favouritenum to 1 
##whatever you type in will be set to favouritenum
favouritenum = easygui.integerbox("What is your favorite number?", default = '1')

##print out whatever you type in or the default num
##need to change int to str,if not error
easygui.msgbox ("You entered " + str(favouritenum))

Output



As you can see,number 1 is already the default number.We are going to choose the default number and click on


In the next post,we will be trying out the number guessing game again,this time with the power of  GUI 

(Post 5/Year 1 Week 1)Learning python part 5:Python basics-Input

Continuing from last post...

Input

So far we have been setting the numbers and string beforehand.In this post,we will will explore how the user can input a number and example of converting temperature Fahrenheit to Celsius with the user input.

##input() for user input,save the user input to the variable somebody
print("Enter your name: ")
somebody = input()
print("Hi", somebody, "how are you today?")

Output

The output below show that the program "pauses" and await the user input



Next,I input my name:eric,the variable somebody will save my input



Converting number with user input

Next,we will be converting temperature Fahrenheit to Celsius with the user input.



print ("This program converts Fahrenheit to Celsius")
print ("Type in a temperature in Fahrenheit: ",)
##save the user input to the variable fahrenheit
fahrenheit = float(input())

##convert the fahrenheit to celsius
celsius = (fahrenheit - 32) * 5.0 / 9


##print out the celsius temperature
print ("That is", celsius,"degrees Celsius")

Output

The output below show that the program "pauses" and await the user input

Next,I input my Fahrenheit temperature,the variable Fahrenheit  will save my input and print it out


In the next post,we will be touching GUI(Graphic User Interface)

(Post 4/Year 1 Week 1)Learning python part 4:Python basics-Basic math and Different data types

In part 4,we will be learning about the basic math of Python

Basic Math

Most of us are familiar with the basic operation with math in school such as +,-,*,/ (addition,subtraction,multiply,divide),but below is a quick recap anyway


##addition
print(8+5)

##subtraction
print(8 - 5)

##multiple
print(8*5)

##divide
print(8/5)

Output



Besides,the usual operator there are also special operator such as exponentation,modulus and increment

##Exponentiation—raising to a power
##If you wanted to multiply 3 by itself 5 times, you could write

print(3 * 3 * 3 * 3 * 3)
print(3 ** 5)

##Modulus—getting the remainder
print(7 % 2)

##Increment and decrements
##similar to addition and subtraction
number = 7
number += 1
print(number)

number = 7
number -= 1
print(number)


Output




Different Data types

Most of the time ,you will be using these three types of data str, int or float
  • float() will create a new float (decimal number) from a string or integer.
  • int() will create a new integer from a string or float.
  • str() will create a new string from a number (or any other type).
So let's try to convert the data below

##1.Changing an int to a float
a = 24
b = float(a)
print(a)
print(b)

##2. Changing a float to an int
c = 38.0
d = int(c)
print(c)
print(d)

e = 54.99
f = int(e)
print(e)
##you can see that f is rounded up
print(f)

##3.Changing a string to a float
a = '76.3'
b = float(a)
print(a)
print(b)

Output



Checking the type

You can also check the type of the data as below

##4.checking the type
a = '44.2'
b = 44.2
print(type(a))##str
print(type(b))##float

output


Wednesday, 4 April 2018

(Post 3/Year 1 Week 1)Learning python part 3:Python basics-String,integer,Variable,Quotes,

For those who are confuse with the last example,refer to last post

It mean that you are are a newbie,jus like me!Hence,we are going to start from the very basic.Do copy and paste the code in the box to run!

First,we are going to start with string by printing the string "Mr morton"

String

#remember the hex sign is equal to comment?
#To print a string you need to enclose it in a  bracket , " "
# print are all in small letter ,if PRINT or Print it will give an error


print("Mr. Morton")

Output

Integer 

Next, we are going to print and add up the integer

#the " " make it become a string,hence it print out 53 + 28
print("53 + 28")

#notice,there is no " " for integer,hence it print out the sum of the two integer
print(53 + 28)

Output


Variable

Next,we are going to print a variable.We are going to first assign a variable and print it out

#assign "Mr. Morton" to teacher
Teacher = "Mr. Morton"

#print the variable 
 print(Teacher)

#we can also assign integer to variable

#The number 5 is assigned to First, and 
#the number 3 was assigned to Second. Then we printed the sum of the two. 

First = 5 
Second = 3 
print(First + Second) 

# you can also assign multiple variable
MyTeacher = "Mrs. Goodyear" 
YourTeacher = MyTeacher 
print(MyTeacher) 
print(YourTeacher) 

Output


Quotes

#Python are also not too fussy on whether you use single quotes or double quote
#printing double quote examples
print("Mr. Morton")

#printing single quotes examples
print('Mr. Morton')

##assigning variable in double quotes
teacher = "Mr. Morton"
print(teacher)

#assigning variable in single quotes


teacher = 'Mr. Morton'
print(teacher)