====== Python Coding Basics ======
==== Print ====
print("Welcome to NetworkChuck Coffee!!!!")
The command below prints to the screen
==== Variables ====
name = input("What is your name? ")
The variable //name// allows for input
==== Concatenation ====
print("Hello " + name + ", thank you for coming in today!\n")
Following on from the variable //name. //The input is printed with the variable //name //in the text. Concatenation allowed for it to be inserted into the text. Plus signs need to be used either side of the quotes when concatenatin
==== IF Statements ====
name = input("What is your name?\n")
if name == "ben" or "Ben":
print("get out of here!")
else:
print("Hello " + name + ", thank you so much for coming in today.\n\n\n")
The IF statement above checks if the input matches "ben" or "Ben". If so the following text is printed to the screen:
''"get out of here!". ''
Otherwise if the input does not match "ben" or "Ben" then the following is printed to the screen:
''"Hello " + name + ", thank you so much for coming in today.\n\n\n"''
Notice how the variable //name //is used with concatenation
==== Or Operator ====
print("Hello, welcome to NetworkChuck Coffee!!!!!!!!!!!!!!")
name = input("What is your name?\n")
if name == "Ben" or name == "Patricia" or name =="Loki":
evil_status = input("Are you evil?\n")
good_deeds = int(input("How many good deeds have you done today?\n"))
if evil_status == "Yes" and good_deeds <4:
print("You're not welcome here " + name + "!! Get out!!")
exit()
else:
print("Hello " + name + ", thank you so much for coming in today.\n\n\n")
The above code reads in the //name //variable. It then uses an IF statement and importantly uses an //or// operator which means if the //name //variable equals:
''"Ben" or "Patricia" or "Loki"''
Further input is required. The User is asked about //evil_status //and //good_deeds//. These are used in the next IF statement. Notice that the next IF statement is nested to the original IF statement. That means that the nested IF is used to continue the logic from the above code, meaning both //evil_status //and //good_deeds //areevaluated. The //and //operator is used to evaluate both. //evil_status //must equal "yes" and //good_deeds// must be less than (<) 4. If so the following is printed to the screen:
''"You're not welcome here " + name + "!! Get out!!"''
Note that the //name// variable is used along with concatenation
IMPORTANT: An //exit() //is placed after the initial IF and nested IF statement so that the script does not try to evaluate the rest of the code. After that an //else// statement is added to print the following to the terminal if none of the above conditions have been matched:
''"Hello " + name + ", thank you so much for coming in today.\n\n\n"''
==== Data Types ====
price = 8
print(type(price))
The above assigns the number 8 to the variable //price.//
The variable is using an integer data type. This can be discovered if the code line below is used. The following will be displayed on screen:
''''
If price was changed to:
price = "8"
The data type would be:
''''
If price was changed to:
price = True
The data type would be:
''''
==== ELIF ====
name = input("What is your name?\n")
print("Hello " + name + ", thank you so much for coming in today.\n\n\n")
menu = "Black Coffee, Espresso, Latte, Cappucino, Frappuccino"
order = input(name + ", what would you like from our menu today? Here is what we are serving.\n" + menu + "\n")
quantity = input("How many coffees would you like?\n")
if order == "Frappuccino":
price = 13
elif order == "Black Coffee":
price = 3
elif order == "Espresso":
price = 4
elif order == "Latte":
price = 5
elif order == "Cappucino":
price = 6
else:
print("We don't have that here...")
price = 0
print(price)
The above code uses elif's. This runs through a list until a condition is matched. The else is used if a condition does not match. In the code above the variable //order// requests input from the menu which is:
''menu = "Black Coffee, Espresso, Latte, Cappucino, Frappuccino"''
If none of the above are matched by the ELIF's then //else //the following is printed to the screen:
''"We don't have that here..."''
In addition a //price //variable is set to each ELIF statement. This is used to print a price based on the menu choice from the //order //variable.
The //price //variable is printed to the screen at the end of the code.