From 605ab16b9c238a6cb998c58ab1e65160bc40052d Mon Sep 17 00:00:00 2001 From: baphim <269010393+baphim@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:34:44 -0400 Subject: [PATCH] feat: created several scratch example files for loops and conditionals --- practice/conditionals_examples.py | 24 ++++++++++++++++++++++ practice/loops_examples.py | 33 +++++++++++++++++++++++++++++++ practice/while_loop_input.py | 24 ++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 practice/conditionals_examples.py create mode 100644 practice/loops_examples.py create mode 100644 practice/while_loop_input.py diff --git a/practice/conditionals_examples.py b/practice/conditionals_examples.py new file mode 100644 index 0000000..fa109bf --- /dev/null +++ b/practice/conditionals_examples.py @@ -0,0 +1,24 @@ +# Comparison operators: '==' equals, '!=' not equal, <, >, <=, >=, and, not +# +# Program moves top to bottom in order +# +# # Set age var +# age = 18 +# # if age var is greater than or equal to 18, print string +# if age >= 18: +# print('You are an adult') +# # else, if age is greater than or equal to 13, print string (fires if conditional above it is not true) +# elif age >= 13: +# print('You are a teen') +# # Else (age is not greater than or equal to 13 or 18) print string (Fires if none of other conditionals are true) +# else: +# print('You are a child') + +# Same structure, triggering elif conditional and adds formatted string +score = 72 +if score >= 90: + print('A grade') +elif score >= 80 and score <90: + print('B grade') +else: + print(f'Your score of {score} needs improvement') \ No newline at end of file diff --git a/practice/loops_examples.py b/practice/loops_examples.py new file mode 100644 index 0000000..92b5530 --- /dev/null +++ b/practice/loops_examples.py @@ -0,0 +1,33 @@ +# # Define a list of items +# fruits = ['apple', 'banana', 'cherry'] +# # Loops through defined list and prints each item as var 'fruit' +# for fruit in fruits: +# print(fruit) + +# # For loops can iterate through the built-in range function as well, this prints all numbers (0 indexed) in the range +# for i in range(5): +# print(i) + +# # While loops iterate while a condition is true +# # Define 'count' var +# count = 5 +# # While count is greater than 0, the indented lines will fire +# while count >0: +# # Print the current count +# print(count) +# # This is shorthand for 'count = count - 1' +# count -= 1 +# # When 'while' condition is no longer true, it continues to this line (This is the end of the loop) +# print("Blast off!") + +# Iterate through a range +for i in range(10): + # If i equals 5 'break' the loop (in this example, only iterates 0, 1, 2, 3, 4) + if i == 5: + # stops the loop with the given condition + break + # if i can be divided by 2 with a remainder of 0 (modulo(this indicates an even number)) + if i % 2 == 0: + # skip even numbers in the loop + continue + print(i) \ No newline at end of file diff --git a/practice/while_loop_input.py b/practice/while_loop_input.py new file mode 100644 index 0000000..1b3bfe9 --- /dev/null +++ b/practice/while_loop_input.py @@ -0,0 +1,24 @@ +# # Defines 'name' var as user input +# name = input('What is your name? ') +# # Print welcome string formatted to repeat user input +# print(f'Hello {name}') + +# import 'random' module, built-in module that creates random numbers +import random +# Define 'secret_number' var as a randomly assigned integer between 1 and 10 +secret_number = random.randint(1, 10) +# Print starting message +print('I am thinking of a number between 1 and 10') +while True: + # Sets 'guess' var to user input converted to integer + guess = int(input('Take a guess: ')) + # if 'guess' equals 'secret_number', prints congrats message and breaks loop + if guess == secret_number: + print('Congrats! You guessed it!') + break + # If 'guess' is less than 'secret_number', print try again message + elif guess < secret_number: + print('Too low, try again.') + # If no other conditions are true, print try again message + else: + print('Too high, try again.') \ No newline at end of file