-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter3+4SelfTaughtProgrammer.py
117 lines (80 loc) · 2.67 KB
/
chapter3+4SelfTaughtProgrammer.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#Chapter 3 Self Taught Programmer
"""print three string"""
print("I love cheese")
print("I love water")
print("I love coffee")
"""write a program that prints a message if a varibale is less than 10
and different message if the varibale is greater than or equal to 10"""
def choose_numb(i):
if i < 10:
print("heck yes")
elif i >= 10:
print("yes")
else:
print("no")
"""write a program that prints a message if a varibale is less than or equal to
10,another mesage if the variable is greater than 10 but less than or equal
to 25, and another message if the variable is greater than 25"""
def choose_numb2(i):
if i <= 10:
print("heck yes")
elif i > 10 <= 25:
print("yes")
elif i > 25:
print("ok")
else:
print("no")
"""create a program that divides two variables and prints the remainder"""
def mod_oper(a,b):
return a%b
print(a%b)
"""create a program that takes two variables, divides them and prints the
quotient """
def quot(a,b):
return a/b
print(a/b)
"""write a program with a variable 'age' assigned to an integer that prints
different strings depending on what integer 'age' is"""
def age(i):
if age <= 25:
print("too young")
elif age > 25 and age <= 30:
print("perfect")
else:
print("do you have a younger brother")
####Chapter 4
"""1. write a function that takes a number as input and returns that number
squared"""
def square_bob(x):
return x**2
# print(square_bob(4))
#16
"""2. create a function that accepts a string as a paramater and prints it"""
def print_string(string):
print(string)
##>>> print_string("problem two")
##problem two
##>>>
"""3. write a function that takes three parameters and two optional params"""
def problem_three(x, y, z, a=10, b=12):
return x + y + z + a + b
result = problem_three(5, 7, 9)
print(result)
"""4. write a program with two functions. The first function should take an
integer as a parameter and return the result of the integer divided by 2,
the second function should take an integer as a parameter and return the
result of the integer multiplied by 4. call the first function , save
the result as a variable, and pass it as a parameter to the second function"""
def cat(u):
return u/2
def dog(d):
return d*4
x = cat(80)
quad = dog(x)
print(quad)
"""5.write a function that converts a string to a float and returns the result"""
def convert(str):
try:
return float(str)
except ValueError:
print("Could not convert the string to a float")