This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conditionals.py
147 lines (113 loc) · 5.03 KB
/
Conditionals.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# The value of 10*4 (40) is greater than 14+23 (37), therefore this
# comparison expression will return the Boolean value of True.
print(10*4 > 14+23) # Should print True
# The letter "t" has a Unicode value of 116 and the letter "s" has a
# Unicode value of 115. Since 116 is not less than 115, the
# comparison of "tall" < "short" (or 116 < 115) is False.
print("tall" < "short") # Should print False
####################
####################
####################
# This function accepts one variable as a parameter
def translate_error_code(error_code):
# The if-elif-else block assesses the value of the variable
# passed to the function as a parameter. The if statement uses
# the equality operator == to test the value of the variable.
# This test returns a Boolean (True/False) result.
if error_code == "401 Unauthorized":
# If the comparison above returns True, then the indented
# line(s) inside the if-statement will run. In this case, the
# action is to assign a string to the translation variable.
# The remainder of the if-elif-else block will not run.
# The Python interpreter will skip to the next line outside of
# the if-elif-else block. In this case, the next line is the
# return value statement.
translation = "Server received an unauthenticated request"
# If the initial if-statement returns a False result, then the
# first elif-statement will run a different test on the value
# of the variable.
elif error_code == "404 Not Found":
# If the first elif-statement returns a True result, then the
# indented line(s) inside the first elif-statement will run.
# After this line, the remainder of the if-elif-else block will
# not run. The Python interpreter will skip to the next line
# outside of the if-elif-else block.
translation = "Requested web page not found on server"
# If both the initial if-statement and the first elif-statement
# return a False result, then the second elif-statement will
# run.
elif error_code == "408 Request Timeout":
# If the second elif-statement returns a True result, then the
# indented line(s) inside the second elif-statement will run.
# After this line, the remainder of the if-elif-else block will
# not run. The Python interpreter will skip to the next line
# outside of the if-elif-else block.
translation = "Server request to close unused connection"
# If the conditional tests above do not produce a True result
# then the else-statement will run.
else:
translation = "Unknown error code"
# The if-elif-else block ends.
# The next line outside of the if-elif-else block will run
# after exiting the block. In this case, the next line returns
# the output from the if-elif-else block.
return translation
# The print() function allows us to display the output of the
# function. To call a function in a print statement, the syntax
# is print(name_of_function(parameter))
print(translate_error_code("404 Not Found"))
# Expected output:
# Requested web page not found on server
####################
####################
####################
# Sets value of the "number" variable
number = 25
# The "number" variable will first be compared to 5. Since it is
# False that "number" is not less than or equal to 5, the expression indented
# under this line will be ignored.
if number <= 5:
print("The number is 5 or smaller.")
# Next, the "number" variable will be compared to 33. Since it is
# False that "number" is equal to 33, the expression indented under
# this line will be ignored.
elif number == 33:
print("The number is 33.")
# Then, the "number" variable will be compared to 32 and 6. Since it
# is True that 25 is less than 32 and greater than 6, the Python
# interpreter will print "The number is less than 32 and/or greater
# than 6." Then, it will exit the if-elif-else statement and the remainder
# of the if-elif-else statement will be ignored.
elif number < 32 and number >= 6:
print("The number is less than 32 and greater than 6.")
else:
print("The number is " + str(number))
# Expected output is:
# The number is less than 32 and greater than 6.
####################
# Skill Group 4
# Use an if statement to calculate a return value
# Use conditional operators
# Recall the arithmetic operators // and %
####################
####################
# This function rounds a variable number up to the nearest 10x value
def round_up(number):
x = 10
# The floor division operator will calculate the integer value of
# "number" divided by x: 35 // 10 will return the integer 3.
whole_number = number // x
# The modulo operator will calculate the remainder value of "number"
# divided by x: 35 % 10 will return the remainder value 5.
remainder = number % x
# If the remainder is greater than 0:
if remainder >= 5:
# Return x multiplied by the (whole_number+1) to round up
return x*(whole_number+1)
# Else, return x multiplied by the whole_number to round down
return x*whole_number
# Calls the function with the parameter value of 35.
print(round_up(35)) # Should print 40
####################
####################
####################