print('hello world')
hello world
def to_f(c):
return ((9/5) * c) + 32
to_f(0)
32.0
to_f(100)
212.0
def to_c(f):
return (5/9) * (f-32)
to_c(32)
0.0
to_f(to_c(25))
25.0
def to_f(c):
if c < -273.15:
raise ValueError("Temperature value is below absolute zero.")
return ((9/5) * c) + 32
to_f(-1)
30.2
def to_c(f):
if f < -459.67:
raise ValueError("Temperature value is below absolute zero.")
return (5/9) * (f-32)
to_c(-300)
-184.44444444444446
def sign(num):
answer = '?'
if num > 0:
answer = '+'
if num < 0:
answer = '-'
if num == 0:
answer = ''
return answer
sign(4)
'+'
sign(-20)
'-'
sign(0)
''
sign(1)
'+'
def sign(num):
if num > 0:
return '+'
if num < 0:
return '-'
if num == 0:
return ''
sign(4)
'+'
sign(0)
''
sign(-20)
'-'
def sign(num):
answer = '?'
if num > 0:
answer = '+'
elif num < 0:
answer = '-'
else:
answer = ''
return answer
sign(4)
'+'
sign(-20)
'-'
sign(0)
''
name = 'Jian'
name[-1]
'n'
word = 'hello'
word[-1]
'o'
def character(name):
if name [-1] == 'i':
return 'friendly'
if name[-1] == 'y':
return 'unfriendly'
character('Fritzi')
'friendly'
character('Fritzy')
'unfriendly'
character('Jian')
def character(name):
indicated = '?'
if name[-1] == 'i':
indicated = 'friendly'
if name[-1] == 'y':
indicated = 'unfriendly'
return indicated
character('Fritzi')
'friendly'
character('Jian')
'?'
# human years: 1 = cat years: 7
def to_cat(human):
if human < 0:
raise ValueError ('not even born')
return human * 7
to_cat(1)
7
to_cat(11)
77
def to_human(cat):
if human < 0:
raise ValueError ('not even born')
return cat / 7
to_human(7)
1.0
to_human(77)
11.0
to_human(0)
0.0
to_cat(0)
0
to_human(1)
0.14285714285714285
to_cat(-1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3084/2807706945.py in <module> ----> 1 to_cat(-1) /tmp/ipykernel_3084/2404523924.py in to_cat(human) 1 def to_cat(human): 2 if human < 0: ----> 3 raise ValueError ('not even born') 4 return human * 7 ValueError: not even born
to_human(-20)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_3084/4099495613.py in <module> ----> 1 to_human(-20) /tmp/ipykernel_3084/2072360683.py in to_human(cat) 1 def to_human(cat): ----> 2 if human < 0: 3 raise ValueError ('not even born') 4 return cat / 7 NameError: name 'human' is not defined
def generation(age):
if age < 0:
indicated = 'not born'
if age == 0:
indicated = 'not born'
if age > 0 < 1:
indicated = 'newborn'
if age > 1 < 3:
indicated = 'toddler'
if age > 3 < 10:
indicated = 'child'
if age > 10 < 19:
indicated = 'teenager'
if age > 19 < 99:
indicated = 'adult'
if age > 99 < 120:
indicated = 'super old'
return indicated
generation(-1)
'not born'
generation(0)
'not born'
generation(0.8)
'newborn'
generation(1.2)
'toddler'
generation(4)
'child'
generation(15)
'teenager'
generation(27)
'adult'
generation(111)
'super old'
list(range(5))
[0, 1, 2, 3, 4]
list(range(0, 5))
[0, 1, 2, 3, 4]
list(range(1, 6))
[1, 2, 3, 4, 5]
list(range(11, 20))
[11, 12, 13, 14, 15, 16, 17, 18, 19]
answer = 1
for num in range(1, 6):
answer = answer * num
print(answer) # Display the current value
answer
1 2 6 24 120
120
answer = 1
for num in range(1, 6):
answer = answer * num
print(answer)
answer
1 2 6 24 120
120
def factorial(n):
answer = 1
for num in range(1, n + 1):
answer = answer * num
return answer
factorial(6)
720
# richtig so?
factorial(0)
1
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)
fact(6)
720
def factfail(n):
return n * factfail(n-1)
factfail(3)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) /tmp/ipykernel_3084/2206540228.py in <module> ----> 1 factfail(3) /tmp/ipykernel_3084/1101104101.py in factfail(n) 1 def factfail(n): ----> 2 return n * factfail(n-1) ... last 1 frames repeated, from the frame below ... /tmp/ipykernel_3084/1101104101.py in factfail(n) 1 def factfail(n): ----> 2 return n * factfail(n-1) RecursionError: maximum recursion depth exceeded
# Exercise 7-4
def factorial(n):
if n < 0:
raise ValueError("falsch")
else:
answer = 1
for num in range(1, n + 1):
answer = answer * num
return answer
factorial(6)
720
factorial(-1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3084/3323783595.py in <module> ----> 1 factorial(-1) /tmp/ipykernel_3084/3986645127.py in factorial(n) 1 def factorial(n): 2 if n < 0: ----> 3 raise ValueError("falsch") 4 else: 5 answer = 1 ValueError: falsch
def fact(n):
if n == 0:
return 1
elif n < 0:
raise ValueError("falsch")
else:
return n * fact(n-1)
fact(6)
720
fact(0)
1
fact(-1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3084/2874172109.py in <module> ----> 1 fact(-1) /tmp/ipykernel_3084/1664805493.py in fact(n) 3 return 1 4 elif n < 0: ----> 5 raise ValueError("falsch") 6 else: 7 return n * fact(n-1) ValueError: falsch
# Exercise 7-5
def fact(n):
if n == 0:
return 1
elif n < 0:
raise ValueError("falsch")
else:
return n * factorial(n-1)
fact(6)
720
fact(0)
1
fact(-1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_3084/2874172109.py in <module> ----> 1 fact(-1) /tmp/ipykernel_3084/3078905230.py in fact(n) 3 return 1 4 elif n < 0: ----> 5 raise ValueError("falsch") 6 else: 7 return n * factorial(n-1) ValueError: falsch
fact(1)
1
# 7.7
def doubler(sequence):
if len(sequence) == 0:
return[]
else:
return [2 * sequence[0]] + doubler(sequence[1:])
doubler([1, 2, 3])
[2, 4, 6]
# i dont understand
doubler(['hi', 'you'])
['hihi', 'youyou']
def factorial(sequence):
answer = []
for element in sequence:
answer = answer + [answer + element]
return answer
factorial(['hi', 'no'])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_3084/3605395138.py in <module> ----> 1 factorial(['hi', 'no']) /tmp/ipykernel_3084/1494033678.py in factorial(sequence) 2 answer = [] 3 for element in sequence: ----> 4 answer = answer + [answer + element] 5 return answer TypeError: can only concatenate list (not "str") to list