def to_f(int):
int= (int * 1.8) + 32
return int
to_f(1)
33.8
to_f(27)
80.6
to_f(100)
212.0
to_f(0)
32.0
def to_c(f):
if f<-273.25:
raise ValueError("Temperature value is below absolute zero.")
f= (f - 32) / 1.8
return f
to_c(32)
0.0
to_c(212)
100.0
to_f(to_c(25))
25.0
to_c(to_f(90))
90.0
to_f(to_c(25))
25.0
def to_f(int):
if int<-273.15:
raise ValueError("Temperature value is below absolute zero.")
int= (int * 1.8) + 32
return int
to_f(-274)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_5729/1242271859.py in <module> ----> 1 to_f(-274) /tmp/ipykernel_5729/4125593850.py in to_f(int) 1 def to_f(int): 2 if int<-273.15: ----> 3 raise ValueError("Temperature value is below absolute zero.") 4 int= (int * 1.8) + 32 5 return int ValueError: Temperature value is below absolute zero.
to_f(-100)
-148.0
to_c(-460)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_5729/3364052293.py in <module> ----> 1 to_c(-460) /tmp/ipykernel_5729/2512362354.py in to_c(f) 1 def to_c(f): 2 if f<-459.67: ----> 3 raise ValueError("Temperature value is below absolute zero.") 4 f= (f - 32) / 1.8 5 return f ValueError: Temperature value is below absolute zero.
to_c(5)
-15.0
to_c(-270)
-167.77777777777777
to_c(-275)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_5729/968533495.py in <module> ----> 1 to_c(-275) /tmp/ipykernel_5729/2135313739.py in to_c(f) 1 def to_c(f): 2 if f<-273.25: ----> 3 raise ValueError("Temperature value is below absolute zero.") 4 f= (f - 32) / 1.8 5 return f 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(5)
'+'
sign(0)
''
sign(-5)
'-'
sign(4)
'+'
def sign(num):
answer = '?'
if num > 0:
answer = '+'
elif num < 0:
answer = '-'
else:
answer = ''
return answer
sign(0)
''
sign(5)
'+'
sign(-8)
'-'
name = 'Paula'
name[-1]
'a'
word = 'hello'
word[-1]
'o'
word[-2]
'l'
word[1]
'e'
word[0]
'h'
'world'[-1]
'd'
def gender(name):
if name[-1] == 'a':
return 'female'
if name[-1] == 'o':
return 'male'
gender('francessco')
'male'
gender('erica')
'female'
gender('maria')
'female'
gender('fabio')
'male'
gender('Apollo')
'male'
gender('Nick')
def gender(name):
indicated = '?'
if name[-1] == 'a':
indicated = 'female'
if name[-1] == 'o':
indicated = 'male'
return indicated
def to_GRD(e):
grd = e * 340.5
return grd
to_GRD(2)
681.0
to_GRD(10)
3405.0
def to_e(g):
e = g / 340.5
return e
to_e(1000)
2.9368575624082234
to_e(1000000)
2936.857562408223
def size(word):
if len(word) < 4:
return 'small'
elif len(word) < 8:
return 'medium'
else:
return 'large'
size('yes')
'small'
size('no')
'small'
size('hello')
'medium'
size('barber')
'medium'
size('barbershop')
'large'
size('pizza')
'medium'
size('pizzaria')
'large'
list(range(5))
[0, 1, 2, 3, 4]
list('5')
['5']
list(range(0,5))
[0, 1, 2, 3, 4]
list(range(1,6))
[1, 2, 3, 4, 5]
list(range(15,18))
[15, 16, 17]
list(range(1,n+1))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_5729/1430468174.py in <module> ----> 1 list(range(1,n+1)) NameError: name 'n' is not defined
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,n+1):
answer = answer * num
return answer
factorial(5)
120
factorial(1)
1
factorial(3)
6
factorial(0)
1
range(1,1)
range(1, 1)
range(1,5)
range(1, 5)
list(range(1,1))
[]
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)
fact(5)
120
def factfail(n):
return n * factfail(n-1)
factfail(5)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) /tmp/ipykernel_5729/1794096580.py in <module> ----> 1 factfail(5) /tmp/ipykernel_5729/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_5729/1101104101.py in factfail(n) 1 def factfail(n): ----> 2 return n * factfail(n-1) RecursionError: maximum recursion depth exceeded
factorial(-5)
1
def factorial(n):
if n < 0:
raise ValueError('Factorial of negative number is undefined')
answer = 1
for num in range(1,n+1):
answer = answer * num
return answer
factorial(-5)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_5729/3591406149.py in <module> ----> 1 factorial(-5) /tmp/ipykernel_5729/3674242494.py in factorial(n) 1 def factorial(n): 2 if n < 0: ----> 3 raise ValueError('Factorial of negative number is undefined') 4 answer = 1 5 for num in range(1,n+1): ValueError: Factorial of negative number is undefined
factorial(0)
1
factorial(5)
120
def fact(n):
if n < 0:
raise ValueError('Factorial of negative number is undefined')
if n == 0:
return 1
else:
return n * fact(n-1)
fact(-5)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_5729/119325979.py in <module> ----> 1 fact(-5) /tmp/ipykernel_5729/3711760238.py in fact(n) 1 def fact(n): 2 if n < 0: ----> 3 raise ValueError('Factorial of negative number is undefined') 4 if n == 0: 5 return 1 ValueError: Factorial of negative number is undefined
fact(0)
1
fact(5)
120
def fact(n):
if n < 0:
raise ValueError('Factorial of negative number is undefined')
if n == 0:
return 1
else:
return n * factorial(n-1)
fact(5)
120
fact(-5)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_5729/119325979.py in <module> ----> 1 fact(-5) /tmp/ipykernel_5729/2850849212.py in fact(n) 1 def fact(n): 2 if n < 0: ----> 3 raise ValueError('Factorial of negative number is undefined') 4 if n == 0: 5 return 1 ValueError: Factorial of negative number is undefined
fact(0)
1
fact(6)
720
word[0]
'h'
word[1:]
'ello'
def doubler(sequence):
if len(sequence) == 0:
return []
else:
return [2* sequence[0]] + doubler(sequence[1:])
doubler([1,2,3])
[2, 4, 6]
#!/usr/bin/python
# Stochastic Texts, copyright (c) 2014 Nick Montfort <nickm@nickm.com>
# Original by Theo Lutz, 1959; translation by Helen MacCormack
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Updated 31 May 2018 to add compatibility with Python 3 (Python 2 still works)
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('')
A GUEST IS DARK. NOT EVERY COUNT IS NEAR.
list(range('mitsa')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_5729/571427614.py in <module> ----> 1 list(range('mitsa')) TypeError: 'str' object cannot be interpreted as an integer
spell('mitsa')
m i t s a
def to_mile(k):
if k < 0:
raise ValueError(' negative distance ')
else:
return k * 0.621371192
to_mile(1)
0.621371192
to_mile(5)
3.10685596
def alcohol(age):
if age < 0:
raise ValueError('Insert a valid age')
elif age < 18:
print('Not allowed for drinking')
else:
print('Allowed for drinking')
alcohol(5)
Not allowed for drinking
alcohol(20)
Allowed for drinking
alcohol(50)
Allowed for drinking
def alc(year):
age = 2021 - year
if age < 0:
raise ValueError('Insert a valid year')
elif age < 18:
print('Not allowed for drinking')
else:
print('Allowed for drinking')
alc(2000)
Allowed for drinking
alc(2005)
Not allowed for drinking
alc(1995)
Allowed for drinking