-
Notifications
You must be signed in to change notification settings - Fork 0
/
beginner.py
725 lines (571 loc) · 23.8 KB
/
beginner.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#HELLO WELCOME TO MY Python Programming Lessons!
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
# Lesson 1: Introduction to If/Else Statements.
# Python If/Else Statement Example
# Prompt the user for input
online = input("Are you online?: ")
# If/Else condition to check the user's input
if online == "yes":
print("Yes, I am online on PS4")
else:
print("No, I am offline on PS4")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
# Lesson 2: Understanding Data Types and Variables
# Python Variables and Data Types Example
# Define variables with different data types
name = "Bob"
age = 21
gpa = 2.2
student = True
# Convert the boolean 'student' to a string
student = str(student)
# Display the type of the 'student' variable
print(type(student))
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 3: Basic Arithmetic and User Input
# Python Arithmetic and User Input Example
# Ask the user for input and store it in variables
item = input("What item do you want? ")
price = float(input("How much is the item? $"))
quantity = float(input("How many? "))
# Perform a calculation using the input values
amount = price * quantity
# Print out the details and total amount
print(f"You ordered {item}, the price of {item} is ${price}.")
print(f"How many {item} you want: {quantity}")
print("Ok!")
print(f"Your total is ${amount}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 4: Calculating Hypotenuse Using Math Module
import math
# Ask the user to input the lengths of the sides
a = float(input("Give me side a of the triangle: "))
b = float(input("Give me side b of the triangle: "))
# Calculate the hypotenuse using the Pythagorean theorem
hypotenuse = math.sqrt(pow(a, 2) + pow(b, 2))
# Print the result
print(f"The hypotenuse of your right triangle is: {hypotenuse}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 5: Age-Based Categorization with Conditional Statements
# Ask the user for their age
userage = input("How old are you?: ")
# Convert the input to an integer
age = int(userage)
# Use conditional statements to categorize the age
if 13 <= age < 20:
print(f"You are {age}, so you are a teenager.")
elif 1 <= age < 13:
print(f"You are {age}, so you are a kid.")
elif age >= 20:
print(f"You are {age}, so you are an adult.")
else:
print(f"Invalid age {age}, please enter a valid number.")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 6: Handling Invalid Inputs and Float Values
# Ask the user for their age
userage = input("How old are you?: ")
# Check if the input is a floating-point number
if "." in userage:
print("Please enter a whole number for your age.")
elif not userage.isdigit():
print("Invalid input. Please enter a valid number.")
else:
# Convert the input to an integer
age = int(userage)
# Use conditional statements to categorize the age
if 13 <= age < 20:
print(f"You are {age}, so you are a teenager.")
elif 1 <= age < 13:
print(f"You are {age}, so you are a kid.")
elif age >= 20:
print(f"You are {age}, so you are an adult.")
else:
print("Invalid input. Please enter a valid number.")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 7: Calculating the Hypotenuse in a Right Triangle
import math
# Ask the user to input the lengths of the two sides of a right triangle
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
# Calculate the hypotenuse using the Pythagorean theorem
hypotenuse = math.sqrt(pow(a, 2) + pow(b, 2))
# Print the hypotenuse
print(f"The hypotenuse of your right triangle is: {hypotenuse}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 8: Using Conditional Statements to Categorize Age
# Prompt the user for their age
userage = input("How old are you?: ")
age = int(userage)
# Conditional statements to determine the age category
if 13 <= age < 20:
print(f"You are {age}, so you are a teenager.")
elif 1 <= age < 13:
print(f"You are {age}, so you are a kid.")
elif age >= 20:
print(f"You are {age}, so you are an adult.")
else:
print(f"Invalid age {age}, please enter a valid age.")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 9: Validating Numeric Input for Age
# Prompt the user for their age
userage = input("How old are you?: ")
# Validate the input and ensure it is a whole number
if "." in userage:
print("Please enter a whole number for your age.")
elif not userage.isdigit():
print("Invalid input. Please enter a valid number.")
else:
age = int(userage)
# Conditional statements for different age categories
if 13 <= age < 20:
print
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 11: Calculating Compound Interest with Input Validation
# Initialize the variables
principle = 0
rate = 0
time = 0
# Validate principle amount
while principle <= 0:
principle = float(input("Enter principle amount: "))
if principle <= 0:
print("Principle can't be less than or equal to zero")
# Validate rate amount
while rate <= 0:
rate = float(input("Enter rate amount: "))
if rate <= 0:
print("Rate can't be less than or equal to zero")
# Validate time amount
while time <= 0:
time = int(input("Enter time amount: "))
if time <= 0:
print("Time can't be less than or equal to zero")
# Calculate the total amount using compound interest formula
total = principle * pow((1 + rate / 100), time)
# Print the result
print(f"The total amount after {time} years is: {total}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 12: Validating Credit Card Numbers (Luhn Algorithm)
# Input and pre-process card number
card_number = input("Enter your card number: ")
card_number = card_number.replace("-", "").replace(" ", "")[::-1]
sum_even = 0
sum_odd = 0
# Step 2: Sum of odd digits
for digit in card_number[::2]:
sum_odd += int(digit)
# Step 3: Double every second digit and sum them
for digit in card_number[1::2]:
doubled = int(digit) * 2
sum_even += doubled if doubled < 10 else 1 + (doubled % 10)
# Step 4: Calculate total sum
total = sum_odd + sum_even
# Step 5: Check if the total modulo 10 is zero (valid card)
if total % 10 == 0:
print("Valid")
else:
print("Invalid")
print(f"Processed card number: {card_number[::-1]}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 13: Iterating Over a Set
fruits = {"apple", "orange", "kiwi"}
# Iterate over the set and print each element
for fruit in fruits:
print(fruit)
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 14: Creating a Shopping Cart with Dynamic Inputs
foods = []
prices = []
total = 0
# Collect food items and prices from the user
while True:
food = input("Enter a food to buy (q to quit): ")
if food.lower() == "q":
break
else:
price = float(input(f"Enter the price of a {food}: $"))
foods.append(food)
prices.append(price)
# Display the shopping cart and calculate total
print("---------Your Cart------------")
for food in foods:
print(food, end=" ")
for price in prices:
total += price
print()
print(f"Your total is ${total}, enjoy!")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 15: Nested Lists and Modifying Elements
fruits = ["apples", "orange", "pear", "banana"]
veggies = ["potato", "carrots", "tomato"]
meats = ["fish", "chicken", "steak"]
food = [fruits, veggies, meats]
fruits[0] = "pineapple"
# Print the modified fruits list
print(fruits)
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 16: Iterating Through a Nested Tuple
num_pad = ((1, 2, 3),
(4, 5, 6),
(7, 8, 9),
("*", 0, "#"))
# Iterate through each row and number in the nested tuple
for row in num_pad:
for num in row:
print(num, end=" ")
print()
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 17: Creating a Quiz Game with Multiple Choice Questions
questions = ("What color are bananas?: ",
"How many continents are there on Earth?: ",
"What is the largest mammal in the world?: ",
"What is the capital of France?: ")
options = (("A. red", "B. car", "C. yellow", "D. green"),
("A. 3", "B. 1", "C. 7", "D. 8"),
("A. dog", "B. hippo", "C. cat", "D. whale"),
("A. Man City", "B. Nothignton", "C. Paris", "D. India"))
answers = ("C", "C", "D", "C")
guesses = []
score = 0
# Loop through each question and present options
for questions_num, question in enumerate(questions):
print("-------------------------------------")
print(question)
for option in options[questions_num]:
print(option)
# Get the user's guess
guess = input("Enter (A, B, C, D): ").upper()
guesses.append(guess)
# Check and respond to the guess
if guess == answers[questions_num]:
score += 1
print("Correct")
else:
print("Incorrect")
print(f"{answers[questions_num]} is the correct answer")
# Display the results
print("-------------------------------------")
print("---------------RESULTS---------------")
print("-------------------------------------")
print("Answers: ", ' '.join(answers))
print("Guesses: ", ' '.join(guesses))
score_percentage = int((score / len(questions)) * 100)
print(f"{score_percentage}%")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 18: Exploring and Manipulating Dictionaries
capitals = {"USA": "Washington D.C.",
"Sudan": "Khartoum",
"India": "New Delhi",
"China": "Beijing"}
# Display all key-value pairs in the dictionary
for country, capital in capitals.items():
print(f"{country}: {capital}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 19: Building a Simple Ordering System
menu = {"pizza": 2.50,
"chicken": 3.00,
"nachos": 2.00,
"fries": 1.00,
"wrap": 5.00,
"soda": 1.50,
"water": 0.50}
cart = []
total = 0
# Display the menu
print("-------------MENU-------------")
for item, price in menu.items():
print(f"{item:10} : ${price:.2f}")
print("------------------------------")
# Take orders from the user
while True:
food = input("Select an item (q to quit): ").lower()
if food == "q":
break
elif food in menu:
cart.append(food)
# Calculate the total and display the order
print("----------Your Order---------")
for food in cart:
total += menu[food]
print(food, end=" ")
# Print the total cost of the order
print(f"\nTotal is: ${total:.2f}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 20: Basic Random Number Guessing Game
import random
# Generate a random number
number = random.randint(1, 2)
# Game loop
while True:
user_guess = int(input("Enter a number 1-2: "))
if user_guess == number:
print("Correct guess!")
break
else:
print("Nope!")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 21: Enhanced Number Guessing Game
import random
# Generate a random number
number = random.randint(1, 2)
# Initial user input
user_guess = int(input("Enter a number 1-2: "))
# Game loop
while user_guess != number:
print("Nope!")
user_guess = int(input("Enter a number 1-2: "))
print("Correct guess!")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 22: Exploring Random Module Functions
import random
# Various random module functionalities
options = ("rock", "paper", "scissors")
cards = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
# Shuffle the cards
random.shuffle(cards)
print(cards)
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 23: Basic Encryption and Decryption
import random
import string
# Prepare character sets for encryption
chars = " " + string.punctuation + string.digits + string.ascii_letters
chars = list(chars)
key = chars.copy()
# Shuffle the key for encryption
random.shuffle(key)
# Encrypt the message
plain_text = input("Enter a message to encrypt: ")
cipher_text = ""
for letter in plain_text:
index = chars.index(letter)
cipher_text += key[index]
print(f"Original message: {plain_text}")
print(f"Encrypted message: {cipher_text}")
# Decrypt the message
cipher_text = input("Enter a message to decrypt: ")
plain_text = ""
for letter in cipher_text:
index = key.index(letter)
plain_text += chars[index]
print(f"Encrypted message: {cipher_text}")
print(f"Original message: {plain_text}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
# Lesson 24: Understanding Variable Scope
```python
# Global scope
a = 1
def hello():
# Local scope
b = 2
print(b)
# Print global variable 'a'
print(a)
# Call function 'hello' which prints local variable 'b'
hello()
# This will result in an error as 'b' is not defined in the global scope
# print(b)
print("Nope!")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 25: Basic Number Guessing Game with Random Module
import random
# Generate a random number between 1 and 2
number = random.randint(1, 2)
# Game loop
while True:
user = int(input("Enter a number 1-2: "))
if user == number:
print("Correct guess!")
break
else:
print("Nope!")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 26: Enhanced Number Guessing Game
import random
# Generate a random number
number = random.randint(1, 2)
# Get the first guess
user = int(input("Enter a number 1-2: "))
# Loop until the correct guess
while user != number:
print("Nope!")
user = int(input("Enter a number 1-2: "))
print("Correct guess!")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 27: Using Random Module for Shuffling and Selection
import random
# List of cards
cards = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
# Shuffle the cards
random.shuffle(cards)
# Display shuffled cards
print(cards)
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 28: Creating a Simple Encryption and Decryption Tool
import random
import string
# Create a list of characters for encryption
chars = " " + string.punctuation + string.digits + string.ascii_letters
chars = list(chars)
key = chars.copy()
# Shuffle the key for encryption
random.shuffle(key)
# Encrypt the message
plain_text = input("Enter a message to encrypt: ")
cipher_text = ""
for letter in plain_text:
index = chars.index(letter)
cipher_text += key[index]
print(f"Original message: {plain_text}")
print(f"Encrypted message: {cipher_text}")
# Decrypt the message
cipher_text = input("Enter a message to decrypt: ")
plain_text = ""
for letter in cipher_text:
index = key.index(letter)
plain_text += chars[index]
print(f"Encrypted original message: {cipher_text}")
print(f"Original message: {plain_text}")
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
import random
def correct(userNum, randNum):
if userNum == randNum:
print(f"Your guess was correct: {userNum}")
return True
else:
print(f"Your guess was wrong. The correct number was: {randNum}")
return False
randNum = random.randint(1, 2)
while True:
userNum = int(input("Guess a random number between 1 and 2: "))
if correct(userNum, randNum):
break
print("Thanks For Playing")
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
#Lesson 29: Handling Exceptions in Python
# Handling exceptions with try-except blocks
try:
numerator = int(input("Enter a number to divide: "))
denominator = int(input("Enter a number to divide by: "))
result = numerator / denominator
except ZeroDivisionError as e:
print(e)
print("You can't divide by zero!")
except ValueError as e:
print(e)
print("Enter only numbers please")
except Exception as e:
print(e)
print("Something went wrong :(")
else:
print(result)
finally:
print("This will always execute")
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 30: File Existence Checking and Sorting Implementation
# Importing the os module, which provides a way of using operating system dependent functionality.
import os
# Define the path to the directory you want to check.
# Note: Always use raw strings (prefix with 'r') or double backslashes for Windows paths to avoid escape character issues.
path = r"C:\icloud\Desktop\text.file"
# Check if the specified path exists using os.path.exists
if os.path.exists(path):
# If the path exists, this block of code will execute
print("Exist")
else:
# If the path does not exist, this block of code will execute
print("no")
# Additional Implementation (not included in the original snippet):
# If the purpose is to sort or organize files within the directory (as suggested by the lesson title),
# you would implement additional logic here to read the files in the directory and sort or organize them
# as required. This could involve listing all files, filtering them based on certain criteria (like file type),
# and then performing actions like moving or renaming them.
"""
#----------------------------------------------------------------------------------------------------------------------------------------------