def to_f(c):
return ((9/5) * c) + 32
to_f(32)
89.6
to_f(150)
302.0
def to_c(f):
return (5/9) * (f-32)
to_c(134)
56.66666666666667
to_c(100)
37.77777777777778
to_f(to_c(25))
25.0
to_c(to_f(90))
90.0
to_c(84)
28.88888888888889
to_f(17)
62.6
to_c(to_f(25))
25.0
to_c(84)
28.88888888888889
def to_f(c):
if c < -273.15:
raise ValueError("Temperature value is below absolute zero.")
return ((9/5) * c) +32
to_f(-290)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_13916/1397739573.py in <module> ----> 1 to_f(-290) /tmp/ipykernel_13916/2519947239.py in to_f(c) 1 def to_f(c): 2 if c < -273.15: ----> 3 raise ValueError("Temperature value is below absolute zero.") 4 return ((9/5) * c) +32 ValueError: Temperature value is below absolute zero.
to_f(-190)
-310.0
def to_c(f):
if f < -20:
raise ValueError("Temperature value is below absolute zero.")
return (5/9) * (f-32)
to_c(-10)
-23.333333333333336
to_c(-24)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_13916/3780980420.py in <module> ----> 1 to_c(-24) /tmp/ipykernel_13916/1490046648.py in to_c(f) 1 def to_c(f): 2 if f < -20: ----> 3 raise ValueError("Temperature value is below absolute zero.") 4 return (5/9) * (f-32) ValueError: Temperature value is below absolute zero.
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)
'+'
name = 'Paula'
name[-1]
'a'
word = 'hello'
word[-1]
'o'
'world'[-1]
'd'
def gender(name):
if name[-1] == 'a':
return 'female'
if name[-1] == 'o':
return 'male'
gender('Apollo')
'male'
gender('Alo')
'male'
gender('Umbrella')
'female'
gender('Nick')
def gender(name):
indicated = '?'
if name[-1] == 'a':
indicated = 'female'
if name[-1] == 'o':
indicated = 'male'
return indicated
gender('Nick')
'?'
gender('Nicka')
'female'
def to_inch(cm):
return ((1 / 2.54) * cm)
to_inch(1)
0.39370078740157477
def to_cm(inch):
return ((1 * 2.54) * inch)
to_cm(0.39370078740157477)
0.9999999999999999
to_cm(45)
114.3
def noise(noise):
indicated = '?'
if 'o' in noise:
indicated = 'Ohohohoooh!'
if 'a' in noise:
indicated = 'Aha!'
if 'ou' in noise:
indicated = 'Bouh!'
if 'i' in noise:
indicated = 'hihi'
if 'm' in noise:
indicated = 'humhum'
return indicated
noise('waho')
'Aha!'
noise('ouhou')
'Bouh!'
noise('mum')
'humhum'
list(range(5))
[0, 1, 2, 3, 4]
list(range(1, 27))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
list(range(0, 5))
[0, 1, 2, 3, 4]
list(range(1,6))
[1, 2, 3, 4, 5]
list(range(-8, 9))
[-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
list(range(145, 147))
[145, 146]
answer = 1
for num in range (1,6):
answer = answer * num
print(answer) #Display the current value
answer
1 2 6 24 120
120
def factorial(n):
answer = 1
for num in range (1, 27):
answer = answer * num
print(answer)
return answer
factorial(26)
1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600 6227020800 87178291200 1307674368000 20922789888000 355687428096000 6402373705728000 121645100408832000 2432902008176640000 51090942171709440000 1124000727777607680000 25852016738884976640000 620448401733239439360000 15511210043330985984000000 403291461126605635584000000
403291461126605635584000000
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)
fact(5)
120
fact(0)
1
fact(-5)
fact(1)
1
fact(2)
2
def factfail(n):
return n * factfail(n-1)
factfail(1)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) /tmp/ipykernel_13916/3795323520.py in <module> ----> 1 factfail(1) /tmp/ipykernel_13916/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_13916/1101104101.py in factfail(n) 1 def factfail(n): ----> 2 return n * factfail(n-1) RecursionError: maximum recursion depth exceeded
factfail(3)
def factorial2(n):
indicated = '?'
if n = 1:
indicated = n * num
return answer
if n > 0:
indicated = ValueError("Value with a negative attitude.")
return indicated
File "/tmp/ipykernel_13916/2973230555.py", line 3 if n = 1: ^ SyntaxError: invalid syntax
^ #aaaaaah
def factfail(n):
if n < 0:
raise ValueError("Value with a negative attitude.")
return n * factfail(n-1)
factfail(-3)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_13916/2426060011.py in <module> ----> 1 factfail(-3) /tmp/ipykernel_13916/3153232792.py in factfail(n) 1 def factfail(n): 2 if n < 0: ----> 3 raise ValueError("Value with a negative attitude.") 4 return n * factfail(n-1) ValueError: Value with a negative attitude.
def doubler(sequence):
if len(sequence) == 0:
return[]
else:
return [2 * sequence[0]] + doubler(sequence[1:])
doubler('tea')
['tt', 'ee', 'aa']
doubler('o')
['oo']
doubler([])
[]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_13916/2612493879.py in <module> ----> 1 stochastic_texts.py NameError: name 'stochastic_texts' is not defined
from random import choice
first_verse = ['I', 'WANNA', 'BE', 'THE', 'VERY', 'BEST',
',', 'LIKE', 'NO ONE', 'EVER', 'WAS', 'TO', 'CATCH',
'THEM', 'IS', 'MY', 'REAL', 'TEST', 'TO', 'TRAIN', 'THEM', 'IS', 'MY', 'CAUSE']
second_verse = [ 'A', 'MISSIONARY', 'IS', 'A', 'MEMBER', 'OF', 'A', 'RELIGIOUS',
'GROUP', 'SENT', 'INTO', 'AN', 'AREA', 'TO', 'PROMOTE']
third_verse = ['I', 'WILL', 'TRAVEL', 'ACROSS', 'THE', 'LAND', ',',
'SEARCHING', 'FAR', 'AND', 'WIDE', 'TEACH', 'THEM', 'TO',
'UNDERSTAND', 'THE', 'POWER', 'THAT IS', 'INSIDE']
chorus = [' AND ', ' YOU ', ' ME ', 'GOTTA ', 'CATCH ', 'EM ', 'ALL ']
print(' ' + choice(first_verse))
print(' ' + choice(chorus))
print(choice(second_verse) + ' ')
print(choice(chorus) + ' ')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_13916/1440982817.py in <module> 14 chorus = [' AND ', ' YOU ', ' ME ', 'GOTTA ', 'CATCH ', 'EM ', 'ALL '] 15 ---> 16 print(' ' + choice(first_verse)) 17 print(' ' + choice(chorus)) 18 print(choice(second_verse) + ' ') TypeError: 'str' object is not callable
from random import choice
subjects = ['COUNT', 'STRANGER', 'LOOK', 'CHURCH', 'CASTLE', 'PICTURE','EYE', 'VILLAGE', 'TOWER', 'FARMER', 'WAY', 'GUEST', 'DAY','HOUSE', 'TABLE', 'LABOURER']
predicates = ['OPEN', 'SILENT', 'STRONG', 'GOOD', 'NARROW', 'NEAR','NEW', 'QUIET', 'FAR', 'DEEP', 'LATE', 'DARK', 'FREE','LARGE', 'OLD', 'ANGRY']
conjunctions = [' AND ', ' OR ', ' THEREFORE ', '. ', '. ', '. ', '. ', '. ']
operators = ['A', 'EVERY', 'NO', 'NOT EVERY']
def phrase():
text = choice(operators) + ' ' + choice(subjects)
if text == 'A EYE':
text = 'AN EYE'
return text + ' IS '
print('')
print(phrase() + choice(predicates) + choice(conjunctions) + phrase() + choice(predicates) + '.')
print('')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_13916/3092557317.py in <module> 12 return text + ' IS ' 13 ---> 14 print('') 15 print(phrase() + choice(predicates) + choice(conjunctions) + phrase() + choice(predicates) + '.') 16 print('') TypeError: 'str' object is not callable
def somename(name):
if name[-1] == 'a':
return 'congratulations! your name ends with the letter a!'
if name[-1] == 'o':
return 'oh my! your name ends with the letter o'
somename('Nico')
'oh my! your name ends with the letter o'
somename('Nikka')
'congratulations! your name ends with the letter a!'