feat: created several scratch example files for loops and conditionals

This commit is contained in:
baphim
2026-09-30 14:34:44 -04:00
parent 1e740c7ff8
commit 605ab16b9c
3 changed files with 81 additions and 0 deletions
+24
View File
@@ -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')
+33
View File
@@ -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)
+24
View File
@@ -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.')