Pages


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)




(Post 2/Year 1 Week 1)Learning python part 2:Number guessing game

Number Guessing Game

##   ##comment
##  importing the module random,jupyter notebook has pre loaded with module

import random

##set the variable secret as a integer 1 to 99
secret = random.randint(1, 99)
print(secret)
##set the variable guess to zero
guess = 0

##set the variable guess to zero
tries = 0

##write the introduction of the number game
print("AHOY! I'm the Dread Pirate Roberts, and I have a secret!")
print ("It is a number from 1 to 99. I'll give you 6 tries. ")

##while guess is not equal to secret and the tries is less than 6
while guess != secret and tries < 6:
    guess = input("What's yer guess?")
    if int(guess) < secret:             ##if guess is less than secret
        print("Too low, ye scurvy dog!")
    elif int(guess) > secret:        ##else if guess is more than secret
        print("Too high, landlubber!")
    elif int(guess) == secret:   ##else if  guess is equal to secret
        print("Avast! Ye got it! Found my secret, ye did!")
        break

    tries = tries + 1      ##tries is equal to tries + 1,so it will increase by 1 per while loop

 
##if guess is equal to secret
if tries == 6:
    print("No more guesses! Better luck next time!")




Output

1st try:


After cheating



Do look out for part 3!

Saturday, 31 March 2018

(Post 1/Year 1 Week 1)Learning python part 1:Hello world

Hello world!

I will be using jupyter notebook for learning python

print("Hello World!")

Output: