Skip to main content

Posts

OS LIBRARY , TIME LIBRARY

  What is the os library? It allows us to "talk" to the console. One of the most powerful things we can do with this library is allow it to clear the console EXAMPLE: import os print("Welcome") print("to") print("Replit") os.system("clear") username = input("Username: ")  Time Library We can import a second library by placing a  ,  after the name of the first library. EXAMPLE: import os, time print("Welcome") print("to") print("Replit") time.sleep(10) os.system("clear") username = input("Username: ")  NOTE:  from replit import audio import os, time def play():   source = audio.play_file('audio.wav')   source.paused = False # unpause the playback   while True:     stop_playback = int(input("Press 2 anytime to stop playback and go back to the menu : ")) # giving the user the option to stop playback     if stop_playback == 2:       source.paused = True # let'...

Return Command

  Return Command The  return  command sends some information back to the part of the code that called it. This means the function call is replaced with whatever was returned  EXAMPLE : def area_square(side1, side2):   area = side1 * side2  return area area = area_square(4, 12) print(area)

Parameters , Call the Subroutine , Adding More Arguments

  Parameters In a subroutine, the  ()  are for the argument (FYI argument is another word for parameter). These are the pieces of information we pass to the code. These can be variable names that are made up for the first time within the argument  () . EXAMPLE : def whichCake(ingredient):   if ingredient == "chocolate":     print("Mmm, chocolate cake is amazing")   elif ingredient == "broccoli":     print("You what mate?!")   else:      print("Yeah, that's great I suppose...") How do we call the subroutine? We call it in the same way as before. However, instead of leaving the  ()  blank, we send the code a message.   EXAMPLE : def whichCake("chocolate"):   if ingredient == "chocolate":     print("Mmm, chocolate cake is amazing")   elif ingredient == "broccoli":     print("You what mate?!")   else:      print("Yeah, that's great I suppose...")  Addin...

Subroutine

  Subroutine A  subroutine  tells the computer that a piece of code exists and to go run that code again and again ... EXAMPLE : def rollDice():   import random   dice = random.randint(1, 6)   print("You rolled", dice)  Call the Subroutine   We need to 'call' the code by adding one more line to our code with the name of the subroutine and the empty  () :  EXAMPLE : def rollDice():   import random   dice = random.randint(1, 6)   print("You rolled", dice) rollDice()

Libraries

  Libraries Libraries are collections of code that other people have written. Video games often use massive libraries (for example:  game engines ) to create the epic water reflections, 3-D graphics, etc.  We are going to start a bit smaller by just generating random numbers. (After all,  random numbers are the basis of most games ).  Random library We can use a library by writing  import  and then the library name.  EXAMPLE : import random myNumber = random.randint(1,100) print(myNumber) How random works In the code below, I have created a variable, 'myNumber'. I am making it equal to a random number given to me by the  randint  (random integer) library. I add the lowest number (1) and the highest number (100) that can be picked and the computer will generate a number at random.

What can range really do?

  What can range  really  do? starting value:  what number do you want to start with? ending value:  the number  after  the number you want to end with (example: if you type 10 as the ending value, the computer will count until 9) increment:  How much should it increase by every time it loops? (example: Do you want to count by 1s, 5s, 10s?)   EXAMPLE :  for i in range(1, 8):   print("Day", i)   Increments We know that this range will start at 0 and continue to 999,999 (which is the number right  before  the ending value written). The number will increase in increments of 25 each time.  EXAMPLE :   for i in range (0, 1000000, 25):   print(i) Counting Backward In this example, we are starting at 10 and counting backward to 0 (because 0 is what comes right  before  the ending value listed), and counting backward 1 each time. EXAMPLE : for i in range(10, -1, -1):   print(i)

FOR LOOP , RANGE

  FOR LOOP  A  while  loop is perfect to use when we  don't  know how many times we want the loop to repeat.  If we have an idea of how many times we want the loop to repeat, we can use a  for  loop to loop code in exactly the same way the  while  loop did.  EXAMPLE :  for counter in range(10):   print(counter) RANGE  The  range  function creates a list of numbers in the range you create. If you only give it one number, it will start at  0  and move to a state where the final number is  one less  than the number in the brackets. In this case, the final number would be  9 .  EXAMPLE :  total = 0 for number in range(100) :   total += number   print(total)