-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypes-and-conversions-quiz.py
More file actions
167 lines (153 loc) · 4.88 KB
/
types-and-conversions-quiz.py
File metadata and controls
167 lines (153 loc) · 4.88 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Quiz: Types and Conversions
Review: concepts/types-and-conversions.md
"""
from _quiz_helpers import normalize_answer
def run_quiz():
print("=" * 60)
print(" QUIZ: Types and Conversions")
print(" Review: concepts/types-and-conversions.md")
print("=" * 60)
print()
score = 0
total = 7
# Question 1
print('Question 1/7: What is the type of "42" (with quotes)?')
print()
print(" a) int")
print(" b) float")
print(" c) str")
print(" d) bool")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! Anything in quotes is a string, even if it")
print("looks like a number.")
else:
print("Incorrect. The answer is c) str.")
print('Quotes make it a string. "42" is text, not a number.')
print()
# Question 2
print("Question 2/7: What does input() always return?")
print()
print(" a) An integer")
print(" b) Whatever type the user types")
print(" c) A string")
print(" d) None")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! input() always returns a string.")
print("You must convert it with int() or float() for math.")
else:
print("Incorrect. The answer is c) a string.")
print("Even if the user types 42, input() returns '42' as text.")
print()
# Question 3
print('Question 3/7: What does "5" == 5 evaluate to?')
print()
print(" a) True")
print(" b) False")
print(" c) Error")
print(' d) "5"')
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! A string and an integer are never equal in Python,")
print("even if they look the same.")
else:
print("Incorrect. The answer is b) False.")
print('The string "5" and the integer 5 are different types.')
print()
# Question 4
print("Question 4/7: Which of these values is 'falsy' in Python?")
print()
print(' a) "hello"')
print(" b) 1")
print(" c) [1, 2, 3]")
print(" d) 0")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "d":
score += 1
print("Correct! 0 is falsy. Other falsy values include: False,")
print('None, "", [], and {}.')
else:
print("Incorrect. The answer is d) 0.")
print("Zero, empty strings, empty collections, None, and False")
print("are all falsy.")
print()
# Question 5
print('Question 5/7: What does int("3.14") produce?')
print()
print(" a) 3")
print(" b) 3.14")
print(" c) ValueError")
print(' d) "3"')
print()
answer = normalize_answer(input("Your answer: "))
if answer == "c":
score += 1
print("Correct! int() cannot convert a float-format string directly.")
print("You would need int(float('3.14')) to get 3.")
else:
print("Incorrect. The answer is c) ValueError.")
print("int() cannot parse a decimal string. Use float() first,")
print("then int().")
print()
# Question 6
print("Question 6/7: What does bool([]) return?")
print()
print(" a) True")
print(" b) False")
print(" c) None")
print(" d) Error")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! An empty list is falsy. bool([]) returns False.")
print("A non-empty list like [1] would return True.")
else:
print("Incorrect. The answer is b) False.")
print("Empty collections are falsy in Python.")
print()
# Question 7
print("Question 7/7: What will this code print?")
print()
print(" x = 10")
print(" y = 3")
print(" print(type(x / y))")
print()
print(" a) <class 'int'>")
print(" b) <class 'float'>")
print(" c) <class 'str'>")
print(" d) Error")
print()
answer = normalize_answer(input("Your answer: "))
if answer == "b":
score += 1
print("Correct! Division with / always returns a float in Python,")
print("even when dividing two integers. Use // for integer division.")
else:
print("Incorrect. The answer is b) <class 'float'>.")
print("The / operator always produces a float. 10 / 3 = 3.333...")
print()
# Final score
print("=" * 60)
pct = round(score / total * 100)
print(f" Final Score: {score}/{total} ({pct}%)")
print()
if pct == 100:
print(" Perfect! You understand Python types and conversions.")
elif pct >= 70:
print(" Good work! Review the questions you missed.")
else:
print(" Keep practicing! Re-read concepts/types-and-conversions.md")
print(" and try again.")
print("=" * 60)
if __name__ == "__main__":
run_quiz()