BigSteve/Python/015_ifconditional.py
2024-06-06 12:15:09 -04:00

26 lines
1.1 KiB
Python

# Basic example of formatting output printed to the console
# Get the user's name
name = input("What is your full name?: ")
# Print a blank line
print()
# Show the user what they typed
print(f"Your name is: {name}")
# Here we will do the "if" conditional. A conditional means
# "compare these things" and "if" means "does this whole thing evaluate to True".
# In this case we are using the string function/method "find()" (see below)
# to try and find a space character in the name and print a message if not found.
# "-1" here means "not found"
# Find method: https://www.w3schools.com/python/ref_string_find.asp
if name.find(" ") == -1:
print("You seem to have only entered a single name. Are you a rock star?")
print()
# Many programming languages include a "ternary" operator/conditional
# This is used to return one value if a conditional evaluates to True
# or a different value if the conditional evaluates to False.
# This is especially useful when inside another function or statement.
# Usage: my_var = "Yes!" if a == b else "No!"
height = 73
print("You are", " not" if height <= 71 else "", " tall!", sep='')