Repository navigation
Expand file tree
/
Copy pathhappy_coding.py
More file actions
253 lines (180 loc) · 6.18 KB
/
Copy pathhappy_coding.py
File metadata and controls
253 lines (180 loc) · 6.18 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import pyfiglet
import os
import subprocess
from termcolor import colored
from google import genai
prohibited_usernames = {
"admin", "administrator", "root", "support", "moderator", "sysadmin", "hostmaster", "postmaster", "api", "www", "ftp", "dns", "http", "mailto", "ssl", "localhost", "abuse", "security", "spam", "spamtrap", "mailer-daemon", "snapchat", "instagram"
}
# Storing registered usernames -
registered_users = [" "]
def terminalcleared():
if os.name == "nt":
cmd = "cls"
else:
cmd = 'clear'
subprocess.call(cmd, shell=True)
def start():
text = pyfiglet.figlet_format("Welcome To Happy Coding!", font="slant")
print(colored(text, "yellow"))
from ascii_magic import AsciiArt, Front
# 1. Load the image
smiley_art = AsciiArt.from_image('SmileyFace.jpeg')
# 2. Print to terminal in Yellow
smiley_art.to_terminal(columns=50, front=Front.YELLOW)
startoptions()
def startoptions():
print("========================================================================")
print("""----> Type i and then press enter to view instructions \n----> Type c and then press enter to continue \n\n""")
response = input()
if response == "i" or response == "I":
show_instructions()
elif response == "c" or response == "C":
login()
else:
print("Invalid input. Please retype your choice\n")
return startoptions()
def show_instructions():
terminalcleared()
instruction = """
=================================
HOW TO USE HAPPY CODING
=================================
STEP 1: GET STARTED
- Start with creating a username - this will allow you to save your progress.
- The details of creating a username are in Step 2.
- Next, select a programming language you would like to learn.
- Then, select the level of difficulty you would like to begin with, and start coding!
STEP 2: CREATE A USERNAME
- Create a username - you may use a combination of letters, numbers and special symbols.
- Note: You may get an error if the username already exists, in that case you must try a different one.
- Once your username is created, you can log in using the 'Enter Your Username' option.
===================
"""
print(instruction)
login()
def login():
guide = """
========================
LOG IN
========================
Do you have a username?
Type Y for Yes
Type N for No
========================
"""
print(guide)
while True:
choice = input("Enter your choice (Y/N): ").strip().lower()
if choice == "y":
login_username()
break
elif choice == "n":
register_user()
break
else:
print("Invalid choice. Please enter 'Y'or N")
# Create New Username -
def create_username(username):
username = username.strip().lower()
for word in prohibited_usernames:
if word in username:
raise ValueError(f"The username contains a prohibited word: '{word}'. Please choose a different username.")
return(username)
# successful username creation -
def register_user():
username = input("Create a New Username: ")
username = create_username(username)
print(f"Username: '{username}' Has been created successfully.")
return username
# Enter the username -
def login_username():
username = input("Enter your Username: ").strip().lower()
if username in registered_users:
print(f"Welcome back, {username}!")
return username
else:
print("Username not found. Please create a username.")
return register_user()
def questions():
print("Welcome to Happy Coding!")
level = input(
"Choose assessment level:\n"
"1. Beginner\n"
"2. Intermediate\n"
"3. Advanced\n"
)
if level == "1":
difficulty = "Beginner"
elif level == "2":
difficulty = "Intermediate"
elif level == "3":
difficulty = "Advanced"
else:
print("Invalid choice. Defaulting to Beginner.")
difficulty = "Beginner"
print(f"\nStarting {difficulty} Assessment...")
client = genai.Client(api_key="AQ.Ab8RN6KZXcListEzSHN8ctMeUQYneDSrVDYJQOnmDnvvInXUsw")
response = client.models.generate_content(
model="gemini-3.8-flash",
contents=f"""
Create 20 {difficulty} coding multiple-choice questions.
Use a mixture of:
- Multiple Choice
- True or False
- Fill in the Blank
Format each question as follows:
Question Type: Multiple Choice
Question: ...
A) ...
B) ...
C) ...
D) ...
Correct Answer: X
###
Question Type: True/False
Question: ...
Correct Answer: True
###
Question Type: Fill in the Blank
Question: Complete the code:
for i in ______(5):
Correct Answer: range
###
Separate each question with ###.
"""
)
questions = response.text.split("###")
score = 0
for question_num, question in enumerate(questions, start=1):
if not question.strip():
continue
print(f"\nQuestion {question_num}/20")
print(question)
answer = input("Your answer (A/B/C/D): ").strip().upper()
try:
correct_answer = (
question.split("Correct Answer:")[1]
.strip()[0]
.upper()
)
if answer == correct_answer:
score += 1
print("✅ Correct!")
else:
print(f"❌ Incorrect. Correct answer: {correct_answer}")
except Exception:
print("⚠ Could not determine correct answer.")
print("\nAssessment Complete!")
print(f"Final Score: {score}/20")
if score <= 7:
result_level = "Beginner"
elif score <= 14:
result_level = "Intermediate"
else:
result_level = "Advanced"
with open("results.txt", "a") as file:
file.write(f" {score}/20, {result_level}\n")
print("Results saved!")
print("Your coding level:", result_level)
start()