Don't wanna be here? Send us removal request.
Text
Instances, also known as objects
Examples of a class
The code - def __init__(self): always has 'self' within the brackets. That's just how it is. It apparently "writes itself into the class."
class Dog:
def __init__(self):
With this method, just above - it runs the code for every printed class and closed parenthesis.
import random class Dog: info = 'a domesticated mammal size = 'the hight, length and with of a dog spicimen' def __init__(zen): print("I'm alive") zen.lucky_number = random.randint(1,10)
terrier = Dog() --- This line of code defines an instance before print. golden_retriever = Dog() print(terrier.lucky_number) --- here, variable is linked to the instance. print(golden_retriever.lucky_number)
Dog()
Dog()
Dog()
Dog()
Each of these repeats a print command for this class and only for classes. an error awaits those who try it elsewhere.
0 notes
Text
Classes - a blueprint essentially.
#Classes.pie - This is a comment.
Classes allow you to prepare various information for merely being ready to be print, such as breed or brand, in this instance. The capital letter and the colon defines a class; in this case - Dog
You cannot print off variables within a class simply as they are (info) in this case, to print off variable - info's data - it's (Dog.info) So that's the class, then dot, then variable, within closed parenthesis.
class Dog: breed = "Yorkshire Terrier" x = 6 print(Dog.info) print(Dog.x)
class Electronic_devices: veriety = 'modem' brand = 'BT' internet_speed = '5gig' print (Electronic_devices.veriety)
Result:
a furry barker 6 modem
Remember not to indent the print.
What I've learned is that you can only print off one variety of information about what I call exhibits, for each print command. That's why I was able to print two pieces of information revolving around the class - Dog: I provided two print commands. Within each of the print brackets having the class - Dog, I followed with a full stop and provided variable. 2 print commands from under the class - Dog, 2 pieces of information.
I think I confused myself with how I was supposed to present the electronic side of thing's. This paragraph won't be relevant for long.
0 notes
Text
Game Loop V2
import random print("Hi! Welcome to the guessing game. Please guess a number between 1 and 100.")
gUess = int(input("What is your guess?: ")) cOrrect_Number = 13 gUess_COunt = 1
while gUess != cOrrect_Number: gUess_COunt = gUess_COunt+1 if gUess < cOrrect_Number: gUess = int(input("Wrong. You need to guess higher. What is your guess?: ")) else: gUess = int(input("Wrong. You need to guess lower. What is your guess?: ")) print(gUess) print(f"Congrats! The right answer was {cOrrect_Number} you took {gUess_COunt} guesses")
0 notes
Text
Flashing Heart Microbit
def on_forever():
basic.show_icon(IconNames.HEART)
basic.pause(100)
basic.clear_screen()
basic.pause(500)
basic.forever(on_forever)
0 notes
Text
Game Loop
guess = int(input("what is your guess?:")) or just guess = 0 correct_number = 5 guess_count = 1
while guess != correct_number: guess_count += 1 guess = int(input("what is oyur guess?:"))
print(f"Congrats! The right answer was {correct_number}. It took you {guess_count} guesses.")
0 notes
Text
Variables
They hold information and can be called anything, but their spaces must be replaced with say, an underscore. You do leave a space between a variable and equals sign and then space before it's information though. For example:
Jake_wallet = 41
When ready, the information of the variable is revealed by typing print and the name of the variable within brackets - no spaces:
print(Jake_wallet)
Here's everything, the complete example:
wallet = 41
print(wallet)
result: 41
0 notes
Text
Return
def double(number): return number * 2
new_number = print(double(5))
print(new_number)
def uppercase(text): return text.upper()
print(uppercase("nick"))
names = ['nick','Jane','sara']
for name in names: print (uppercase(name))
Result
10 None NICK JANE SARA
0 notes
Text
Return
def double(number): return number * 2
print(double(5))
You can return all kinds of code: strings boolians etc
def double(number): return number * 2
new_number = print(double(5))
print(new_number)
Result:10
None
def MyDogInfo(x): return x.upper()
print (MyDogInfo("Dog is Josh and is 7"))
Result: Dog is Josh and is 7
0 notes
Text
Failed parameter training
def MyDogInfo (age,age0): print(age + age0)
MyDogInfo
def MyDogName (Josh): print('Josh')
MyDogName
Result: invalid syntax
0 notes
Text
Parameters
Further down, the reason I haven't simply typed ("num,num") is because those two variables have to be different, however minor (and variables can't be actual numbers either.) They are going to represent the two numbers to be added together.
hello("john")
def add_numbers(num,num0): print(num + num0)
add_numbers(1,2)
Numbers can't be variables, interesting lesson
Dog Info
def MyDogInfo (age,name): print(f"My name is {name} and I'm {age}")
MyDogInfo(7,'Josh')
0 notes
Text
Function
a way of giving name to a chunk of code They need to be called out after print with just the variant followed by closed brackets with nothing in them.
'def' - defines and effectively creates a function
def bark(): - the colon signals the end of the function print("woof woof!")
bark() - here the function is called and executes the code in the function
this means that bark is woof woof!
def Hello(): print("Hello Nick")
Hello()
0 notes
Text
Dictionaries
Considered a storage database for the coders to utilise.
Line one defines dictionary
Line two looks up and chooses from dictionary
you can mix integers, boolians (...I think) and strings amongst the 'keys' and 'values' of the dictionary.
0 notes
Text
Loops
for number in range(10): print("Hello") print("Hi there")print(number)
for number in range(41): print((number + 1) * 2, Hi there')
for is recognised by the system to set up variables to be replicated.
in and range(), including number within it's brackets, determines how much replication variables undergo upon the print command.
Interger numbers have been found to amount higher during replication; but it is unclear whether this is because the int number is left without commas that would turn it into a string, or if there is something key in the first line of code shown above. Upon experimentation, you can not have range without in (not int :D) and vice versa.
Both of the above gives the result of successive numbers proceeding each other in twos, starting with 2 and ending in 40
0 notes
Text
Lists
Lists are initiated by square brackets surrounding subjects to print. I cannot think of what to refer to the subjects as, yet.
The code below isn't meant to make sense. I solely placed numbers within a list of favourite movies to deviate from video instructions. This makes it easy for my novice coding brain to solidify what it is that makes the code function properly.
favourite_movies = [4,3,2,6,4,7,]
print(favourite_movies[5])
fave_numbers = ['The Revenant', 'Scott Pilgrim vs the World',] print(fave_numbers[1])
print(len(favourite_movies)) favourite_movies.append("The Dark Knight")
del(favourite_movies[1]) print(favourite_movies)
del(favourite_movies[3]) print(favourite_movies)
del(favourite_movies[3]) print(favourite_movies)
Result is 3
The result is 13
Trying to simultaneously use len while adding items, will simply count the number of items without adding more.
The first print command is only shows data relevant to the first code. If you only want the most updated item information revealed, leave the print command at the very end.
0 notes
Text
This needs some notes
import random
lucky_number = random.randint(1,5)
if lucky_number == 1: lucky_text = 'your day was a little depressing' if lucky_number == 2: lucky_text = 'your day was adequat' if lucky_number == 3: lucky_text = 'your day was great' if lucky_number == 4: lucky_text = 'your day was fantastic!' if lucky_number == 5: lucky_text = 'your day was the best :)' print(f'{lucky_number}') print(f'{lucky_text}')
0 notes
Text
Clarification on why it worked
import random
answer = random.randint(1,3)
if answer == 1: print("I could feel better.") if answer == 2: print("I feel good :)") if answer == 3: print("I feel great!")
0 notes
Text
This is Correct
import random
answer = random.randint(1,3)
if answer == 1: print("I could feel better.") if answer == 2: print("I feel good :)") if answer == 3: print("I feel great!")
0 notes