# In this code, a file is imported using a curl statememt. The file is named ranbow.txt.
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
# [ In this code, the rainbow.txt file is opened in 'r' mode using the open() method. Then, the lines of the file are
# saved as items of list, which is named rainbow_lines, by using the readlines method.] Open rainbow.txt in read mode & read as list with .readlines()
rainbow_file = open("rainbow.txt","r")
rainbow_lines = rainbow_file.readlines()
# In this code, the lines of rainbow_file are sorted in alphabetical order using the sort() method. Then
# with a for..in loop, each line is printed including the formatting they had.
# [ ] sort rainbow_colors list, iterate the list to print each color
rainbow_lines.sort()
for color in rainbow_lines:
print(color)
rainbow_file.close()
# [ In this code, a file is imported using the curl statement. The file is named mean_temp.] The Weather: import world_mean_team.csv as mean_temp.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
# [ In this code, the mean_temp.txt file is opened in 'r' mode with the open() method. The file is read line by
# line with the readline() method and then the contents of the file are printed. The file contents are converted
# into a list of items using the split() method. The created list is printed. ] The Weather: open file, read/print first line, convert line to list (splitting on comma)
mean_temp = open("mean_temp.txt","r")
headings = mean_temp.readline()
print(headings)
headings = headings.split(',')
print(headings)
# [ ] The Weather: use while loop to print city and highest monthly average temp in celsius
# In this code, the mean_temp file is read line by line each saved as a string. Then, a while loop is used
# to print each city with its highest monthly average temp. To do this, a list was created with the strings
# of the mean_temp file. The file is closed.
city_temp = mean_temp.readline()
while city_temp:
city_list = city_temp.split(',')
print(city_list[0] + ": " + city_list[2])
city_temp = mean_temp.readline()
mean_temp.close()
# In this code, a file is imported using the curl statement. The file is named pi.txt
# [ ] use curl to download https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi as pi.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi -o pi.txt
# In this code, the pi_file is opened in read mode with the open() method. Then, the user is asked to enter
# his/her name. Then the lenght of the name is stored in the variable seed. The pointer is moved to the value of
# seed using the seek() method.The variable digit contains the value the user is asked to guess. The variables
# correct and incorrect are initialized at 0.
# [ ] Set up the project files and initial values
pi_file = open("pi.txt","r")
name = input("What is your name?")
print("Hi, " + name.capitalize() + "!")
seed = len(name)
pi_file.seek(seed)
digit = pi_file.read(1)
guess = input('Enter a single digit guess or "q" to quit: ')
correct = 0
wrong = 0
# In this code, a while loop is used to test if guess is a digit string. If digit is not a number, then the seek()
# method moves the pointer to the next character. If guess is a number, then the if/else statement checks if
# the number is correct or not. The number of correct and incorrect answers are displayed. The pi_file is closed
# using .close().
while guess.isdigit():
if digit == ".":
digit = pi_file.read(1)
elif digit == "\n":
seed += 1
pi_file.seek(seed)
digit = pi_file.read(1)
else:
if guess == digit:
print(guess,"is correct")
correct += 1
else:
print(guess, "is incorrect")
wrong += 1
guess = input("Enter another digit guess or \"q\": ")
print("\nCorrect:",correct,"\nIncorrect:",wrong)
pi_file.close()