Last active
January 11, 2018 00:44
-
-
Save Erisa/cde8412379ff4d68ad774879a32ec40b to your computer and use it in GitHub Desktop.
Some assignment for some college thing :teehee:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("┌-────────────────────────────-┐ _ ") | |
print("│ Procedural Programming Demo │ ( ) _ ") | |
print("│ With Python! │ (_` )_('> ") | |
print("├─────────────────────────────-┤ (__,~_)8 ") | |
print("│ │ _YY_ ") | |
print('""""|""|""^^^|^^^^^^^^^^^"""|""^^^^^^^"""|"^') | |
def procedure_demo(): | |
print("\n\nThis demo is actually a procedure in itself, but lets demo one anyway!") | |
print("(Comments will help here") | |
# Below we're creating a new procedure with the 'def' keyword. | |
# When this procedure is ran, it will calculate something and return the value. | |
def calculation(): | |
return int((12 * 8) / (2 * 7)) | |
# Now instead of copy/pasting that sum everywhere, we can just do this: | |
result = calculation() | |
print("The result of the procedure is: ", result) | |
def parameters(): | |
# This is our function which takes a parameter inside (). | |
def add(num1, num2): | |
# Inside the function we can do what we want. | |
return num1 + num2 | |
print(add(12,28)) | |
def libraries(): | |
# Here, we're going to import the 'random' library. | |
# Which will allow us to generate a random number. | |
import random | |
# Now we are using the imported command. | |
number = random.random() * 100 | |
print("Have a random number from 1 to 100:", number) | |
def loops(): | |
# int() is used to ensure the input is an integer (Whole number) | |
iterations = int(input("How many times should I iterate? ")) | |
for i in range(iterations): | |
# Iterations in Python start counting at 0, so add 1. | |
print("You are on iteration", i + 1) | |
print("Iteration ended.") | |
def variables(): | |
# Here we are assigning "Hello world" to the variable newvar. | |
newvar = "Hello world" | |
# When we call newvar, it will have the contents we specified. | |
print("Current variable contents is:", newvar) | |
# Here the variable will be changed to whatever the user specifies. | |
newvar = input("What would you like to set it to?") | |
# And now it's different! | |
print("Variable is now:", newvar) | |
def localvars(): | |
# Here we are going to set a value: | |
num1 = 100 | |
def squaresum(num1, num2): | |
first = num1 * num1 | |
second = num2 * num2 | |
return first + second | |
result = squaresum(3,4) | |
# ˅ If you uncommented this line, it would return an error. | |
# print(second) | |
# ^ This is because "second" is just a local variable for that function. | |
print("The sum of your numbers squared is:", result) | |
def conditions(): | |
num = int(input("Enter a number: ")) | |
# Only executes if the number is lower than 18. | |
if num < 18: | |
print("Your number is less than 18!") | |
# If the number is 18 or higher but under 23... | |
elif num < 23: | |
print("Your number is between 18 and 22") | |
elif num < 80: | |
print("Your number is between 23 and 79!") | |
# If none of the others are true.... | |
else: | |
print("Your number is 80 or higher!") | |
print("\n\nWelcome to the Python demo!") | |
# Hello, this is an example comment. | |
# Comments are lines which start with '#' | |
print("To ensure you understand the logic behind this demo, please read the code comments.") | |
while True: | |
print("\n\nWhich demo would you like to view?") | |
print(" - [Procedures] - Basic demonstration of procedures/functions in Python.") | |
print(" - [Parameters] - Extension of the procedures example, shows parameter usage.") | |
print(" - [LocalVars] - Example of local and global variables with regards to functions.") | |
print(" - [Libraries] - Programming libraries and how to import/use them.") | |
print(" - [Variables] - Basic variable idea demonstration with examples.") | |
print(" - [Loops] - The various types of loops in Python, allowing to repeat code easily.") | |
print(" - [Conditions] - Conditional statements for example if and else and elif.") | |
print("\nPlease enter a valid demo name (Inside the [] ) or 'exit' to quit.") | |
choice = input(">> ") | |
if (choice.lower() == "exit"): | |
break | |
elif (choice.lower() == "procedures"): | |
procedure_demo() | |
elif (choice.lower() == "loops"): | |
loops() | |
elif (choice.lower() == "parameters"): | |
parameters() | |
elif (choice.lower() == "libraries"): | |
libraries() | |
elif (choice.lower() == "loops"): | |
loops() | |
elif (choice.lower() == "variables"): | |
variables() | |
elif (choice.lower() == "localvars"): | |
localvars() | |
elif (choice.lower() == "conditions"): | |
conditions() | |
else: | |
print("That isn't a valid response!") | |
input("\nPress enter to continue...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment