Code^3

How to print/output in Python: use the print command. It may print static text, or dynamic variables depending on what is encapsulated in the print() parentheses. Below code takes two numbers and outputs their sum.

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
output = num1+num2
print(str(output)) # prints the output variable after converting to string

Functions are very useful in Python (and other programming languages) because they perform some task on a number of set parameters. Defining functions can reduce code redundancy and can be done with the def keyword.

String concatenation also exists. A string may be appended to another string, as shown in the example below:

def concatenate(str1,str2):
    return str1+str2
# testing function concatenate()

first = input("First string: ")
second = input("Second input: ")
print(concatenate(first,second))

Note that the local variables (parameters of concatenate, str1 and str2) need not be the same as the global variables ‘first’ and ‘second’ here. In the last line, the print function calls concatenate, which with its assigned parameters sets str1 and str2 equal to first and second respectively. The function returns the concatenation of these two strings, then print function outputs to the terminal.

Additionally, in Python, there are numerous libraries and functions that can be imported into programs to improve functionality. For example,

import turtle

imports the turtle graphics library for usage. (this module is really fun to experiment with)

Lastly, ‘if’ statements in Python are used to evaluate if the contained expression is true or false. The code branches: If “true”, then the code takes the brance directly under ‘if’, and if “false”, the code takes the other “else” branch.

‘if’ statements has much more functionality than just “true” or “false”. An example is shown below, having more than just two branches.

num = int(input("Enter your number: "))
if num < 0:
    print("The number is negative!")
elif num == 0: # if-elif-else chain provides more than just 2 possible branches
    print("The number is exactly zero")
elif 0 < num <= 10:
    print("I could eat this amount of tangerines in one sitting...")
else: # I can't eat more than 10 tangerines at once :(
    print("That's pretty BIG")

Finally, here is my own “quiz” including questions related to the above discussed material, complete with in-line comments demonstrating how the code works.

The quiz shortens the sample code from teacher website using a defined function and a for loop looping through questions and answers in order. It looks more aesthetic to the eye and one can more easily look past all the redundancy.

import getpass, sys

def q_with_a(prompt): # use this every time to print question and get user response
    print("Question: " + prompt)
    useranswer = input()
    return useranswer

question_bank = ["Name the Python output command mentioned in this lesson",
                 "If you see many lines of code in order, what would College Board call it?",
                 "Describe a keyword used in Python to define a function?",
                 "What command is used to include other functions that were previously developed?",
                 "What command is used to evaluate correct or incorrect response in this quiz?",
                 "Each 'if' command contains an '____' to determine a true or false condition?",
                 "What python library is fun to experiment with?",
                 "What is Hayden's top score in Crossy Road?"
                 ]
answers = ["print","sequence","def","import","if","expression","turtle","1000"]
questions = len(question_bank) # length function gets number of q's
correct = 0 # initialize correct answer count

print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
q_with_a("Are you ready??") # start it off

for i in range(questions): # for loop with iterator to go through each of the question/answer sets (numbered with i)
    userans = q_with_a(question_bank[i]) # get the user answer
    if userans == answers[i]: # same as the defined answer in answers list
        print(userans + " is correct!")
        correct += 1 # add 1 to score
    else: # not the same as the defined answer
        print(userans + " is incorrect!")
print("Congrats, " + getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
percentage = correct/questions * 100 # calculate percentage of questions correct
print("This score as a percentage is " + str(percentage) + "%.") # output

Hello, Hayden running /opt/homebrew/opt/python@3.11/bin/python3.11
You will be asked 8 questions.
Question: Are you ready??
Question: Name the Python output command mentioned in this lesson
print is correct!
Question: If you see many lines of code in order, what would College Board call it?
sequence is correct!
Question: Describe a keyword used in Python to define a function?
def is correct!
Question: What command is used to include other functions that were previously developed?
import is correct!
Question: What command is used to evaluate correct or incorrect response in this quiz?
if is correct!
Question: Each 'if' command contains an '____' to determine a true or false condition?
expression is correct!
Question: What python library is fun to experiment with?
turtle is correct!
Question: What is Hayden's top score in Crossy Road?
1000 is correct!
Congrats, Hayden you scored 8/8
This score as a percentage is 100.0%.