Friday, December 12, 2014

1.3.7 Conclusion Questions

1. Q: Sometimes code using an iterative loop can be written without a loop, simply repeating the iterated code over and over as separate lines in the program. Explain the disadvantages of developing a program this way.

2. Q: Name a large collection across which you might iterate.

3. Q: What is the relationship between iteration and the analysis of a large set of data?


1. A: The advantages of writing code this way is that you can more easily determine the number times something happens and it doesn't have to be linked to anything else. One disadvantage is that you may have to write the code out a lot of times and your file could become very cluttered.

2. A: A large collection you might iterate across could be a list of numbers or letters like in my hangman and lottery programs.

3. A: Iteration and analysis of a large data set both share to repeating of certain processes until a desired result occurs.



What I Did


    I created the Lottery program really fast and decided to focus mainly on the Hangman game. The lottery game just goes through the winning list and checks if the item is in the guessed list and if it is it add one to the common numbers. The big problem is tthis is that you have to input the two lists when running the function. You cannot just type matches(12345,12345) you have to type matches([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]). I find this annoying and would fix it if i really wanted to improve this function. 

   I made the Hangman game slowly and originally started with working code just as simple as the lottery function.  I decided to make it more complex by asking you to input the words and guesses instead of taking them when running the function. I then had to fix a problem where if there were two or more of the same letter in the program it wouldn't report the second number and you still have to tell it a letter twice. There is still a problem I forgot to mention in the final code regarding some that I keep forgetting about. I will update this post when I remember. I had to make raw_input equal to input because of the version of python i was using at home to work on this.


1.3.7 Hangman Code File

from __future__ import print_function

# Hangman Game By Randy Cole Last modified on 12-12-14
# I am aware the variable atemps is spelled wrong
# Some problem I didn't have time to fix:
# 1. The game doesn't support spaces or special characters as they would be given to you in real hangman.

def hangman_display():
 # initializes most variables
    raw_input = input # needed in my home version of python to use raw_input
    blanks = [] # what the user sees to help the guessing
    word = '' # secret word
    guess = '' # the users guess
    atempts = 8 # the amount of tries left

    word = raw_input('What is the secret word?(no spaces only letters)\n').lower()

    while len(word) < 1 or word.isalpha() != True: # checks formating and asks for reentry if wrong
     word = raw_input('Oops you didnt enter anything or had a space or a nonalphabetic character.\nTry again.').lower()

    newword = list(word) # initializes now to take the value after the wrong input check

    for item in word: # sets the variable that displays blanks
        blanks += '-'

    while atempts > 0:
        print(''.join(blanks)) # shows the blanks to the player
        guess = raw_input('Guess a letter.\nIt has to be letters and 1 character only.\nYou must guess multiple letters twice.').lower() # asks for guess
        if len(guess) > 1: # checks if the user inputted anything
            guess = raw_input('Try again with only one letter(press enter to continue).\n').lower() # asks again
        elif guess.isalpha() != True: # checks if user inputed a number or other character that isn't a letter
            guess = raw_input('Must be only letters(Press enter to continue).\n').lower() # asks again
        elif guess in word: # checks if they guessed a correct letter
            pos = word.find(guess) # finds the letter in the word
            blanks[pos] = guess # sets the user display to include the letter
            newword[pos] = '1' # changes a letter to a number
            word = ''.join(newword)  # changes new word back into a string with the number at the letter guessed
            if word.isnumeric() == True: # check is all letters have been guesssed (the word will only have 1s)
                print('You Win!') #tells user they one and ends game
                break
        else: # you guessed a wrong letter removes one attempt and prints remaining ones
            atempts -= 1
            if atempts > 0:
                print('You have', atempts, ' attempt(s) left.')
    else: # if you run put of attempts
        print('You Lose!')

1.3.7 Lottery Ticket Code File

def matches(ticket, winners):
    common = 0
    for item in winners:
        if item in ticket:
            # adds one to common if a number is shared
            common += 1
    print(common)

No comments:

Post a Comment