Pages


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!