# Charles Edgar
# November 9, 2021
# In this lesson I learned how to download files and adjust them in different ways to complete different
# tasks.
# In this lesson I struggles with using a while loop to adjust lines of text (task 2). Fir some reason, I
# had in my head that I needed to use a for loop to iterate through each .readline(). I eventually got
# what I wanted to happen while using the a loop, but it was happening too many times, because the task
# was being done for each item in each line. I knew the for loop was the issue, so I deleted it, but kept
# the code inside of it the same, so that now it was only in the while loop.
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
# [ ] Open rainbow.txt in read mode & read as list with .readlines()
rainbow_file = open('rainbow.txt','r')
rainbow_colors = rainbow_file.readlines()
# [ ] sort rainbow_colors list, iterate the list to print each color
rainbow_colors.sort()
for color in rainbow_colors:
print(color)
# Close rainbow_file
rainbow_file.close()
# [ ] 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
# [ ] The Weather: open file, read/print first line,
mean_temps = open('mean_temp.txt','r')
headings = mean_temps.readline()
print(headings)
# [ ] convert line to list (splitting on comma)
headings = headings.split(',')
print(headings)
# [ ] The Weather: use while loop to print city and highest monthly average temp in celsius
city_temp = mean_temps.readline()
# Converts each .readline() to a list and prints the necessary indexes
while city_temp:
city_list = city_temp.split(',')
print(city_list[0] + ': ' + city_list[2])
city_temp = mean_temps.readline()
# Close mean_temps
mean_temps.close()
# [ ] 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
# [ ] Set up the project files and initial values
# Open file in read mode
pi_file = open('pi.txt','r')
# Get user input and print message
username = input("What is your name?")
print('Hi, ' + username.capitalize() + '!')
# Use length of username to determine which digit of pi is read
seed = len(username)
pi_file.seek(seed)
digit = pi_file.read(1)
# Get user input for guess value
guess = input('Enter a single digit guess or "q" to quit:')
# Initialize counter variables
correct = 0
wrong = 0
while guess.isdigit():
# digit is adjusted if it is not a number
if digit == ".":
digit = pi_file.read(1)
elif digit == "\n":
seed += 1
pi_file.seek(seed)
digit = pi_file.read(1)
# Tests if user input is equal to digit
else:
# Print message and increments associated counter variable
if guess == digit:
print('Correct')
correct += 1
else:
print('Incorrect')
wrong += 1
# Get user input for guess value
guess = input('Enter a single digit guess or "q" to quit:')
# Print correct and wrong values
print('\nCorrect:',correct,'\nIncorrect:',wrong)
# Close pi_file
pi_file.close()