"""
Learning Goals:
 - Use programming to specify algorithms to computers
   - Interact with user input through interpreter input and files

 - Understand how computers organize data at a low level
   - Computers use folders and files to store data hierarchically
   - Files store data in strings using specific formats
"""

# Guessing game: computer thinks of a number x between 1-10,
# repeatedly asks the user to guess it until they get it right

import random
# set our secret number with some kind of random generator
secret = random.randint(1, 10)
correctGuess = False
# repeat until the user guesses the number:
while correctGuess == False:
    # ask the user for a guess
    guess = int(input("Enter a number between 1 and 10: "))
    # compare guess to secret number
    if guess == secret:
        print("Good job, you got it!")
        # if they match - we're done, exit the loop
        correctGuess = True
    elif guess < secret:
        print("Nope, it is larger than that.")
    else: # guess > secret
        print("Nope, it is smaller than that.")


# This program counts the number of lines a character has in a script

# Read the script from the file- make sure it's in the same directory!
f = open("week4-example.txt", "r")
script = f.read()
f.close()

char = "HAMLET"
count = 0

# Go through each line of the script
for line in script.split("\n"):
    # Each character name ends with a period; look for the index of that period
    periodIndex = line.find(".")
    # The name is all characters up to that index
    name = line[:periodIndex]
    if name == char:
        # If it's the character we're looking for, update the count!
        count = count + 1
print(count)


# Additional example: write a new script with just your character's lines!
f = open("week4-example.txt", "r")
script = f.read()
f.close()

char = "HAMLET"
newScript = ""
for line in script.split("\n"):
    periodIndex = line.find(".")
    name = line[:periodIndex]
    if name == char:
        newScript = newScript + "\n" + line
    
f = open("my-script.txt", "w")
f.write(newScript)
f.close()