Reminders:
1) Use print statements check what your code is doing (or not doing) as you write it
2) Comment your code!
# load packages
import numpy as np
import matplotlib.pyplot as plt
import time
# what does np.random.rand() do?
# How would you check?
#
# create an array of 10 random numbers from [0,1)
# uniform distribution
x = np.random.rand(10)
y = np.random.rand(10)
np.random.rand?
Syntax errors are a very common type of error. Most syntax errors are typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your code for any of these.
# try printing a statement without parentheses
print 'hello' # this will result in a syntax error
File "<ipython-input-63-e742bb42f403>", line 2 print 'hello' # this will result in a syntax error ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello' # this will result in a syntax error)?
A NameError means that Python tried to use a variable or function name. If it hasn't been defined at this point, you get the error.
z = 4
print(z)
# if z is not define, this will raise a NameError
4
def circle_fun(x,y):
if (x**2 + y**2) <= 1:
print(x,y)
## Value and Raise Errors
for i in range(10):
circle_fun(x[i],y[i])
# circle_fun(x,y) will raise an error because the
# function does not know how to apply definition to
# array
0.6163668686266298 0.07077505678262752 0.560053829450884 0.6712877647106675 0.5893929739855491 0.7310838026211307 0.638534977241193 0.28923916163126795 0.15555404330663136 0.4267436940297492 0.1300435282846174 0.29841941801568717 0.8749603224123859 0.344256453714884 0.09427313995296471 0.5506315951521389 0.6958709175461909 0.2730299641931907
def getones(n):
# check if number is less than zero
if(n <= 0):
# tell the user to pass a positive integer
#print('Please enter a positive integer') -- this works
raise(TypeError('This function only accepts positive integers'))
# check if n == 1
elif(n == 1):
# if n == 1, return that number
return n
# if the previous conditions do not apply, then
# continue
else:
# if n is divisible by 7, divide it by 7
if n % 7 == 0:
print(n, 'is divisible by 7, dividing now')
mynum = n/7
return getones(mynum)
# if n is not divisible by 7, add one
else:
print(n, 'is not divisible by 7, adding one now')
mynum=n+1
return getones(mynum)
getones(0)
------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-78-bb95f7b784fb> in <module> ----> 1 getones(0) <ipython-input-77-233b3754738d> in getones(n) 4 # tell the user to pass a positive integer 5 #print('Please enter a positive integer') -- this works ----> 6 raise(TypeError('This function only accepts positive integers')) 7 # check if n == 1 8 elif(n == 1): TypeError: This function only accepts positive integers
def temp(a):
for i in range(a):
print('*',2)
mystar = '*'
print(mystar)
for i in range(5):
#mystar = mystar + '*'
print(mystar + '*')
print(mystar - '*')
* **
------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-95-fd7397107d27> in <module> 4 #mystar = mystar + '*' 5 print(mystar + '*') ----> 6 print(mystar - '*') TypeError: unsupported operand type(s) for -: 'str' and 'str'