Transcript PPT 4

PPT 4
Variables: (assignment)

A variable: holds a value.

A space in RAM we give a name to

A lot like parameters, only we create them within the function

Then we can use them inside the function
def f(x): #simple and silly example of using a variable
y = 3 # y only exists within this function
return(x + y)
Variables:
def f(x):
y=3
y=y+x
return(y**2)
f(5)
# Do the right side first, then put
# that value into the left side.
More examples:
def calcvol(length,width,depth):
area = length * width
vol = area * depth
return(vol)
#area only exists inside this function
def bankaccount(x,add):
dollars = 2.57
print("Currently, you have " + str(dollars) + " in your bank account")
if add == 1:
dollars = dollars + x # evaluate right, then assign to left
else:
dollars = dollars - x
return("you now have " + str(dollars) + " in your bank account")
#again, function ends when the return statement is executed.
print(bankaccount(0.10,1)
print(bankaccount(1.0, 0)
Shortcuts
>>> x = 4
>>> x +=2
>>> x
6
>>> x -=7
>>> x
-1
>>> x *= 32
>>> x
-32
>>> x /=8
>>> x
-4.0
>>>
Example:
def f(p1,p2):
if p1>p2:
x = p1-p2
else:
x = p2-p1
if (x%2) == 1:
x+=1
x/=2
return(x)
print(f(7,2))
print(f(24,82))
# x is now what value?
# Now what is x?
# and now what is x?
Input from user:

What if we want to ask the user to input something?

We can use input!

answer = input(“Do you love this class?”)


Takes what you typed in and
places it in the variable,
answer.
It’s always a string unless we
convert it to another type
if (answer == “yes”):
return (“You get an A!”)
else:
return(“You fail.”)
Input
def getage_1():
x = input(“How old are you?”)
if x< 16: return(“You can’t
drive”)
else:
return(“Get your license”)
print(getage_1())
def getage_2():
x = input(“How old are you?”)
x = int(x)
if x < 16:
return(“You can’t drive”)
else:
return(“Get your license”)
print(getage_2())
• The input function always returns a string type.
• what you type in in response to the question is always
considered a string.
• If you want it to be an int, you must convert it.
Why should I use a variable here?:
def f():
k = input("What is your favorite color?")
if (k == 'blue'):
return("You love harmony, are reliable, sensitive and always make an effort to think of others.
You like to keep things clean and tidy and feel that stability is the most important aspect in life.")
elif (k == 'red'):
return(“you live life to the fullest and are tenacious and determined in their endeavors.")
elif (k == 'green'):
return("you are often affectionate, loyal and frank. Green lovers are also aware of what
others think of them and consider their reputation very important. ")
elif (k == 'yellow'):
return("you enjoy learning and sharing your knowledge with others. Finding happiness
comes easy to you and others would compare you to sunshine. ")
elif (k == 'purple'):
return("you are artistic and unique. You have a great respect for people but at times can
be arrogant.")
elif (k == 'brown'):
return("you are a good friend and try your hardest to be reliable and dependable. Flashy
objects are not something you desire; you just want a stable life.")
print(f())
Variables and Random numbers:

You’ve used variables with Random Numbers

To generate random numbers between x and y:
randrange(x,y)

To store the random number:
randvar = randrange(0,100)


generates a random number between 0 and 99
(inclusive) and stores it in randvar

Now you can use randvar again and again throughout
the function
To see what number is generated:
print (randvar)