# Zhi Yuan "Oscar" Lin
# 8 November 2021
# In this lab, it was mainly review of what were taught in the previous lessons. The following are some of what were
# reviewed: commenting, importing by using the !curl command, opening with a mode, closing a file by using the file method
# ".close()", using other file methods such ".read()", ".readline()", and ".readlines()", initializing and using variables,
# and using built-in functions such as "len()", "int()", "input()", and "print()".
# There were no major problems or questions throughout this lab.
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
# This program imports a file named "rainbow.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
# [ ] Open rainbow.txt in read mode & read as list with .readlines()
# The variable "cities_file" is assigned to the return file object from using the open() function to open the file named
# "cities.txt" in read mode, and text mode by default.
rainbow_text = open('rainbow.txt', 'r')
# The variable "rainbow_lines" is assigned to the value assigned to the rainbow_text variable, after each line of the value
# assigned to the rainbow_text variable is converted into a list item due to the file method ".readlines()".
rainbow_lines = rainbow_text.readlines()
# [ ] sort rainbow_colors list, iterate the list to print each color
# The variable "sort_rainbow_lines" is assigned to the given value when the value assigned to the rainbow_lines variable
# is sorted alphabetically due to the sorted() function.
sort_rainbow_lines = sorted(rainbow_lines)
# This function displays the value assigned to the sort_rainbow_lines variable in the output.
print(sort_rainbow_lines)
# This program removes the reference created by the file open() function that is assigned to the rainbow_text variable, by
# using the file method ".close()".
rainbow_text.close()
# [ ] The Weather: import world_mean_team.csv as mean_temp.txt
# This program imports a file named "mean_temp.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
# [ ] The Weather: open file, read/print first line, convert line to list (splitting on comma)
# The variable "mean_temp_file" is assigned to the return file object from using the open() function to open the file named
# "mean_temp.txt" in read mode, and text mode by default.
mean_temp_file = open('mean_temp.txt', 'r')
# The variable "headings" is assigned to the string form of the first line of the value assigned to the mean_temp_file
# variable due to the file method ".readline()".
headings = mean_temp_file.readline()
# This function displays the value assigned to the headings variable in the output.
print(headings)
# The variable "headings_list" is assigned to the value assigned to the headings variable after it is split into a list of
# strings due to headings variable being attach to the string method ".split()". However, in this case, characters are
# splited by every "," character, instead of the default character " ".
headings_list = headings.split(',')
# This function displays the value assigned to the headings_list variable in the output.
print(headings_list)
# [ ] The Weather: use while loop to print city and highest monthly average temp in celsius
# The variable "city_temp" is assigned to the string form of the first line of the value assigned to the mean_temp_file
# variable due to the file method ".readline()".
city_temp = mean_temp_file.readline()
# The variable "city_infi" is assigned to the value assigned to the city_temp variable after it is split into a list of
# strings due to city_temp variable being attach to the string method ".split()". However, in this case, characters are
# splited by every "," character, instead of the default character " ".
city_info = city_temp.split(',')
# In this case, the while loop is followed by the variable "city_temp". Hence, it will evaluate true as long there there
# is a value assigned to the city_temp variable. Additionally, the codes that are indented and follow the while loop
# will be part of the loop. Thus, the loop starts with the 1st substring of the value assigned to the city_info variable,
# the string ": " and the 3rd substring of the value assigned to the city_info variable, in the output. Subseqeuntly,
# the city_temp variable is reassigned to the string form of the next line of the value assigned to the mean_temp_file
# variable due to the file method ".readline()". Additionally, the city_info variable will be reassigned to the city_temp
# variable after it is split into a list of strings due to city_temp variable being attach to the string method ".split()".
# However, in this case, characters are splited by every "," character, instead of the default character " ". This loop will
# continue until the while loop evaluates false.
while city_temp:
print(city_info[0] + ': ' + city_info[2])
city_temp = mean_temp_file.readline()
city_info = city_temp.split(',')
# This program removes the reference created by the file open() function that is assigned to the mean_temp_file variable, by
# using the file method ".close()".
mean_temp_file.close()
# [ ] use curl to download https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi as pi.txt
# This program imports a file name "pi.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi -o pi.txt
# [ ] Set up the project files and initial values
# The variable "pi_file" is assigned to the return file object from using the open() function to open the file named
# "pi.txt" in read mode, and text mode by default.
pi_file = open('pi.txt', 'r')
# The variable "name" will be assign to the entry the user inputs when presented with the string "Enter your name: ".
name = input("Enter your name: ")
# This function displays the string "Hi", and the value assigned to the name variable in the output.
print("Hi", name)
# The variable "seed" is assigned to the given value when determining the length of the value assigned to the name variable
# by using the len() function.
seed = len(name)
# This line of code moves the pointer to the position of the value assigned to the seed variable in the value assigned to
# the pi_file variable, due to the file method ".seek()".
pi_file.seek(seed)
# The variable "digit" is assigned to the first character that is given when the pi_file variable is attached to the file
# method ".read()".
digit = pi_file.read(1)
# The variable "guess" will be assigned to the entry the user inputs when presented with the string "Enter a single digit
# guess or "q" to quit".
guess = input('Enter a single digit guess or "q" to quit')
# The variable "correct" is assgined to the integer "0".
correct = 0
# The variable "wrong" is assgined to the integer "0".
wrong = 0
# In this case, the while loop is written with the keyword "True". Hence, it cannot evaluate anything but true. Additionally,
# the codes that are indented and follow the while loop will be part of the loop. Thus, the loop starts with an IF statement.
# The IF statment determines if the value assigned to the digit variable is equal to the string "." If true, the digit
# variable will be reassigned to the next character of the value assigned to the pi_file variable due to the file method
# ".read()". If false, the program moves on and the elif statement determines if the value assigned to the digit variable is
# equal to the escape character "\n". If true, the seed variable will be reassigned to the given value, when determining the
# length of the value assigned to the name variable by using the len() function, incremented by the integer "1", after the
# given value is changed into an integer due to the int() function. Subsequently, there is a line of code that moves the
# pointer to the position of the value assigned to the seed variable in the value assigned to the pi_file variable, due to
# the file method ".seek()". If also false, the program moves on and the next elif statement will determine if the value
# assigned to guess variable is equal to the value assigned to the digit variable. If true, the string "correct", that is
# within the parentheses of a print() function, will be display in the output. Additionally, the correct variable will be
# increment by the integer "1". If also false, the program moves on and the next elif statement determines if the value
# assigned to the guess variable is equal to the string "q". If true, the loop will end due to the keyword "break". If also
# false, the program moves on to the else statement with the string "incorrect", that is within the parentheses of a print()
# function, will be display in the output. Additionally, the wrong variable will be increment by the integer "1". This loop
# continues until the user inputs an entry, that will be assign to the guess variable, that is equal to the string "q".
while True:
if digit == ".":
digit = pi_file.read(1)
elif digit == "\n":
seed = int(len(name)) + 1
pi_file.seek(seed)
elif guess == digit:
print("correct")
correct += 1
elif guess == "q":
break
else:
print("incorrect")
wrong += 1
guess = input('Enter a single digit guess or "q" to quit')
# This function displays an empty line in the output due to the empty argument.
print()
# This function displays the value assigned to the name variable, the string "'s ", and the string "score: " in the output.
print(name + "'s " + "score: ")
# This function displays the string "Correct", and the value assigned to the correct variable in the output.
print("Correct:", correct)
# This function displays the string "Wrong:", and the value assigned to the wrong variable in the output.
print("Wrong:", wrong)
# This program removes the reference created by the file open() function that is assigned to the pi_file variable, by
# using the file method ".close()".
pi_file.close()