Skip to content

Commit 8b32c29

Browse files
author
fares-ds
committed
First commit
0 parents commit 8b32c29

File tree

87 files changed

+13086
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+13086
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

01_quiz_game/quiz_game.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import random
2+
3+
4+
# Dictionary that contain the feature as key and his inventer as value
5+
question_dict = {'Ballpoint pen': 'Biro Brothers', 'Jet Engine': 'Sir Frank Whittle',
6+
'Gramophone': 'Thomas Alva Edison', 'Internal Combustion Engine': 'Otto',
7+
'The Spinning Jeny': 'James Hargreaves', 'the small pox vaccine': 'Edward Jenner',
8+
'Railway air brakes': 'George Westinghouse', 'Electric streetcar': 'Thomas Davenport',
9+
'Electric Generator': 'Michal Faraday', 'Gun Powder': 'Roger Bacon'
10+
}
11+
12+
mark = 0
13+
# Turning the dict keys into a random list
14+
question_list = list(question_dict.keys())
15+
random.shuffle(question_list)
16+
# loop through the random list of questions
17+
for i, key in enumerate(question_list):
18+
print(40 * '-')
19+
print(f'{i + 1}- How invent {key}')
20+
21+
# Give the user 4 choices, be sure that the choices are random and includes the right answer
22+
choices = list(question_dict.values())
23+
random.shuffle(choices)
24+
choices.remove(question_dict[key])
25+
choices = choices[:3]
26+
choices.append(question_dict[key])
27+
random.shuffle(choices)
28+
# Print the choices to the user
29+
for a, b in enumerate(choices):
30+
print(f" {a + 1}- {b}")
31+
32+
# Taking the user answer
33+
response = int(input('Enter your choice : '))
34+
35+
# Evaluating the user answer
36+
if choices[response - 1] == question_dict[key]:
37+
mark += 1.0
38+
else:
39+
mark -= 0.5
40+
41+
print(question_dict[key])
42+
print('-------------------------')
43+
print(f"You have got {mark}/10")

02_number_game/number_game.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import random
2+
3+
4+
def game():
5+
# generate a random number between 1 and 10
6+
secret_number = random.randint(1, 10)
7+
8+
guesses = []
9+
while len(guesses) < 5:
10+
try:
11+
# get a number from the player
12+
user_guess = int(input("Enter a number between 1 and 10 "))
13+
except ValueError:
14+
print("That's not a number!")
15+
else:
16+
# compare guess to secret number
17+
if user_guess == secret_number:
18+
print("Well done! You guess it")
19+
break
20+
elif user_guess > secret_number:
21+
print("Too High...")
22+
elif user_guess < secret_number:
23+
print("Too Low...")
24+
else:
25+
print("You miss :-(")
26+
guesses.append(user_guess)
27+
else:
28+
print(f"You didn't get it! My number was {secret_number}")
29+
30+
play_again = input('Do want to play again? Y/N ')
31+
if play_again.lower() != 'n':
32+
game()
33+
else:
34+
print("Bye!!")
35+
36+
37+
game()

03_shopping_list/shopping_list.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# make a list to hold onto our new_items
2+
shopping_list = []
3+
4+
5+
# show the list items
6+
def show_items(lst):
7+
print("Here is your list: ")
8+
for item in lst:
9+
print(f"--> {item}")
10+
11+
12+
# show a helping list
13+
def show_help():
14+
# print out instructions on how to use the app
15+
print("What should we pick up at the store?")
16+
print('''
17+
Enter 'DONE' to stop adding items.
18+
Enter 'HELP' for this help.
19+
Enter 'SHOW' to see your current list.
20+
''')
21+
22+
23+
def add_items(item):
24+
# add new new_items to our List
25+
shopping_list.append(item)
26+
print(f"Added {item}. List now has {len(shopping_list)} items.")
27+
28+
29+
show_help()
30+
31+
while True:
32+
new_item = input("> ")
33+
34+
# be able to quit the app
35+
if new_item == 'DONE':
36+
break
37+
38+
elif new_item == 'SHOW':
39+
show_items(shopping_list)
40+
continue
41+
42+
elif new_item == 'HELP':
43+
show_help()
44+
continue
45+
46+
add_items(new_item)

04_letter_game/letter_game.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import os
2+
import random
3+
import sys
4+
5+
# make a list of words
6+
words = [
7+
'apple', 'banana', 'orange', 'coconut',
8+
'strawberry', 'lime', 'grapefruit',
9+
'lemon', 'kumquat', 'blueberry', 'melon'
10+
]
11+
12+
13+
def clear():
14+
if os.name == 'nt':
15+
os.system('cls')
16+
else:
17+
os.system('clear')
18+
19+
20+
# draw spaces, guessed letters and strikes
21+
def draw(bad_guesses, good_guesses, secret_word):
22+
clear()
23+
24+
print(f"Strikes : {len(bad_guesses)}/7")
25+
print('')
26+
27+
for letter in bad_guesses:
28+
print(letter, end='')
29+
print('\n\n')
30+
31+
for letter in secret_word:
32+
if letter in good_guesses:
33+
print(letter, end='')
34+
else:
35+
print('_', end='')
36+
print('')
37+
38+
39+
# take a guess
40+
def get_guess(bad_guesses, good_guesses):
41+
while True:
42+
guess = input("Guess a letter: ").lower()
43+
44+
if len(guess) != 1:
45+
print("You can only guess a single letter!")
46+
elif guess in good_guesses or guess in bad_guesses:
47+
print("You've already guess that letter!")
48+
elif not guess.isalpha():
49+
print("You can only guess letters!")
50+
else:
51+
return guess
52+
53+
54+
def play(done):
55+
clear()
56+
secret_word = random.choice(words)
57+
bad_guesses = []
58+
good_guesses = []
59+
60+
while True:
61+
draw(bad_guesses, good_guesses, secret_word)
62+
guess = get_guess(bad_guesses, good_guesses)
63+
64+
if guess in secret_word:
65+
good_guesses.append(guess)
66+
found = True
67+
for letter in secret_word:
68+
if letter not in good_guesses:
69+
found = False
70+
if found:
71+
print("You win!")
72+
print(f"The secret_word was {secret_word}")
73+
done = True
74+
else:
75+
bad_guesses.append(guess)
76+
if len(bad_guesses) == 7:
77+
draw(bad_guesses, good_guesses, secret_word)
78+
print("You lost!")
79+
print(f"The secret_word was {secret_word}")
80+
done = True
81+
if done:
82+
play_again = input("Play again? Y/n").lower()
83+
if play_again != 'n':
84+
return play(done=False)
85+
else:
86+
sys.exit()
87+
88+
89+
def welcome():
90+
start = input("Press enter/return to start or Q to quit ").lower()
91+
if start == 'q':
92+
print("Bye!")
93+
sys.exit()
94+
else:
95+
return True
96+
97+
98+
print("Welcome to Letter Guess!")
99+
100+
done = False
101+
102+
while True:
103+
clear()
104+
welcome()
105+
play(done)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
3+
"""
4+
- Enter 'a' to add a movie, 'l' to see your movies, 'f' to find a movie, and 'q' to quit:
5+
6+
- Add movies
7+
- See movies
8+
- Find a movie
9+
- Stop running the program
10+
11+
Tasks:
12+
[x]: Decide where to store a movies
13+
[x]: What is the format of a movie?
14+
[x]: Show the user the main interface and get their input
15+
[x]: Allow users to add movies
16+
[x]: Show all their movies
17+
[x]: Find a movie
18+
[x]: Stop running the program when they type 'q'
19+
"""
20+
21+
movies = []
22+
23+
24+
def clear():
25+
if os.name == 'nt':
26+
os.system('cls')
27+
else:
28+
os.system('clear')
29+
30+
31+
def add_movies():
32+
clear()
33+
message = "Enter the name, the director and the year of release of the movie: (name, director, year) "
34+
print('-' * len(message))
35+
movies_input = input(message).split(',')
36+
movies.append({'name': movies_input[0], 'director': movies_input[1], 'year': int(movies_input[2])})
37+
print()
38+
39+
40+
def show_movies():
41+
clear()
42+
for movie in movies:
43+
show_movies_details(movie)
44+
45+
46+
def show_movies_details(movie):
47+
message = f"The title : {movie['name']}, Directed by: {movie['director']}, In the {movie['year']}"
48+
print('-' * len(message))
49+
print(message)
50+
print('')
51+
52+
53+
def find_movie():
54+
clear()
55+
search_input = input("Enter the Title or the Director of the movie you are searching for : ").lower()
56+
for movie in movies:
57+
if movie['name'].lower() == search_input or movie['director'].lower() == search_input:
58+
print("Here's your movie")
59+
print(f"The title : {movie['name']}, Directed by: {movie['director']}, In the {movie['year']}")
60+
else:
61+
print("Not found")
62+
63+
64+
def menu():
65+
clear()
66+
while True:
67+
68+
user_input = input("Enter 'a' to add a movie, 'l' to see your movies, 'f' to find a movie, and 'q' to quit: ")
69+
70+
if user_input == 'a':
71+
add_movies()
72+
73+
elif user_input == 'l':
74+
show_movies()
75+
76+
elif user_input == 'f':
77+
find_movie()
78+
79+
elif user_input == 'q':
80+
print("Stopping program...")
81+
break
82+
else:
83+
print("Unknown command-please try again. ")
84+
85+
86+
menu()

06_building_an_interactive_dictionary/data.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import json
2+
from difflib import get_close_matches
3+
4+
data = json.load(open("data.json"))
5+
6+
7+
def close_matches(w):
8+
list_of_matches = get_close_matches(w, data.keys(), 20, 0.7)
9+
return list_of_matches
10+
11+
12+
def define(w):
13+
w = w.lower()
14+
list_matches = close_matches(w)
15+
16+
if w in data:
17+
return data[w]
18+
19+
elif w.capitalize() in data:
20+
return data[w.capitalize()]
21+
22+
elif w.upper() in data:
23+
return data[w.upper()]
24+
25+
elif len(list_matches) > 0:
26+
message = f"Please enter a valid word, if the word you meant is this enter 'Y', otherwise enter 'N'"
27+
print('=' * len(message))
28+
print(list_matches)
29+
yn = input(message)
30+
print('=' * len(message))
31+
32+
if yn.capitalize() == "Y":
33+
user_input = input('Enter the word from the list : ')
34+
return define(user_input)
35+
36+
elif yn.capitalize() == "N":
37+
return "The word doesn't exist. Please double check it."
38+
39+
else:
40+
return "We didn't understand your entry."
41+
42+
else:
43+
return "The word doesn't exist. Please double check it."
44+
45+
46+
sentence = "Enter word: "
47+
print('=' * len(sentence))
48+
word = input(sentence)
49+
output = define(word)
50+
if type(output) == list:
51+
for item in output:
52+
print('=' * len(item))
53+
print(item)
54+
else:
55+
print('=' * len(output))
56+
print(output)

0 commit comments

Comments
 (0)