generated from mwc/lab_names
I created the python shapes files utilizign
functions and loops. I modified the greeting program. Checkpoint1: A value is what something is and a name represents the value. For istance when we talk about pi, we just say the name pi, but the value is 3.14.. Variables are extremely useful in programming in order to minimize confusion, it helps to not use literals when possible. For instance if I am programming an arduino, I set a pin value so that if I change my circuit I dont need to rewrite that pin value all over my program, I instead use a variable to represent the pin value so I only need to change the variable line. Variables in code are useful and are reusable values that allow us to speed up programming, make code more readable and more efficient. Checkpoint2: A function is like a variable in several ways, you need to define the function (just like when you write a variable), functions are used to make repeatable, clean lines of code that reduce confusion. Functions actually allow for their own in-block temporary variables called "parameters", paired with arguments they give functions great flexibility and power. Functions are useful to do many different things in a program. For instance if I wanted to use several different area formulas, we might want to break them down into seperate functions. Or we can use functions inside of one another to make code cleaner and easier.
This commit is contained in:
parent
e1fe100aa1
commit
5e13684a23
|
@ -1,6 +1,12 @@
|
|||
# circle_area.py
|
||||
# --------------
|
||||
# By MWC Contributors
|
||||
import math #imports pi constant
|
||||
|
||||
##Refactor with a function##
|
||||
print("This program will calculate the area of a circle.")
|
||||
radius = float(input("What is the circle's radius? "))
|
||||
def circleArea(radius):
|
||||
return math.pi * (radius ** 2)
|
||||
|
||||
print(circleArea(radius))
|
|
@ -1,7 +1,7 @@
|
|||
# greetings.py
|
||||
# ------------
|
||||
# By MWC contributors
|
||||
# By Louis Cooper
|
||||
|
||||
my_name ="Chris"
|
||||
my_name = input("What is your name? ")
|
||||
greeting = "Hello, " + my_name
|
||||
print(greeting)
|
||||
|
|
18
shapes.py
18
shapes.py
|
@ -1,11 +1,23 @@
|
|||
# shapes.py
|
||||
# ---------
|
||||
# By MWC contributors
|
||||
# By Louis Cooper
|
||||
|
||||
from turtle import *
|
||||
|
||||
def triangle(side_length):
|
||||
pass
|
||||
for i in range(3):
|
||||
forward(side_length)
|
||||
right(120)
|
||||
|
||||
|
||||
|
||||
|
||||
def rectangle(height, width):
|
||||
pass
|
||||
for i in range(2):
|
||||
forward(width)
|
||||
right(90)
|
||||
forward(height)
|
||||
right(90)
|
||||
|
||||
#rectangle(200, 100)
|
||||
#triangle(100)
|
Loading…
Reference in New Issue