BYU logo Computer Science

To start this assignment, download this zip file.

Lab 4f — Random and Coiteration

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab4f.

Review on random

4 minutes

Here is a short reminder of how to use random, from the guide on random:

# random choice from a list
fruits = ['pear','mango','banana','strawberry','kumquat']
selection = random.choice(fruits)
# 'mango'

# random integer between 1 and 10
number = random.randint(1, 10)
# 8

# random float between 0 and 1
number = random.random()
# 0.4622434

# two random letters from a word, without repeats (no replacement)
name = 'Washington'
two_letters = random.sample(name, 2)
# ['W', 'g']

Guess a number

5 minutes

We have given you code in guess_a_number.py that mostly implements a guessing game:

  • the computer picks a number between 1 and 100
  • you enter a guess until you get it right
  • the computer tells you if you have it right, need to go higher, or need to go lower

The code looks like this:

# Be sure to import random


def pick_number() -> int:
    """
    Returns a random number between 1 and 100
    """
    # Write code here
    pass


def guess(number: int):
    while True:
        your_number = int(input("Guess: "))
        # Write code here
        # "You got it!" if correct
        # "Lower" if too high
        # "Higher if too low


def play_game():
    print("Welcome! I picked a number between 1 and 100. Can you guess it?")
    number = pick_number()
    guess(number)


if __name__ == '__main__':
    play_game()

You need to:

  • import random

  • Write code for pick_number()

  • Write the rest of the code for guess()

Discuss with the TA:

  • Show a solution.
  • Is there anything you don’t understand about random functions?
  • Make sure everyone understands every line of code we supplied.

Rock, paper, scissors

5 minutes

We have written a game of rock, paper, scissors in rock_paper_scissors.py. To complete the code, you need to write the get_computer_choice() function, which gets the computer’s random choice when it is their turn.

Discuss with the TA:

  • Show a solution.
  • Is there anything you don’t understand about random functions?
  • Make sure everyone understands every line of code we supplied.

Review on coiteration with zip

Here is a short reminder of how to use zip, from the guide on coiteration with zip:

fruits = ['apple', 'pear', 'peach', 'surplus']
prices = [0.25, 0.40, 10.0]
for fruit, price in zip(fruits, prices):
    print(f'{fruit} : ${price}')

This prints:

apple : $0.25
pear : $0.4
peach : $10.0

Add numbers

10 minutes

We have given you some code in add_numbers.py that mostly adds two lists of numbers together:

def add_numbers(list1: list[int], list2: list[int]) -> list[int]:
    """
    Takes two lists of numbers.
    Returns a new list that adds each corresponding item together from
    list1 and list 2.
    """
    # Write code here
    pass



def enter_list() -> list[int]:
    numbers = []
    print("Enter a list of numbers!")
    while True:
        number = input("Number: ")
        if number == '':
            break
        numbers.append(int(number))

    return numbers


def main():
    list1 = enter_list()
    list2 = enter_list()
    results = add_numbers(list1, list2)
    print(results)


if __name__ == '__main__':
    main()

You need to write the add_numbers() function. When you run the code, it should print something like this:

Enter a list of numbers!
Number: 1
Number: 2
Number: 3
Number:
Enter a list of numbers!
Number: 4
Number: 5
Number: 6
Number: 7
Number:
[5, 7, 9]

Discuss with the TA:

  • How did you implement this function? Show a solution and discuss.
  • What happens if one list is longer than the other? Why?
  • Is there anything you don’t understand about zip?
  • Make sure everyone understands every line of code we supplied.

Repeat

15 minutes

We have given you some code in repeat.py that is supposed to do the following:

  • gets a person to input a word or phrase
  • creates a list of random numbers from 1 to 10, with one number per character in the word/phrase
  • prints a new word/phrase with every character repeated a random number of times

For example, if we run the program, we could see this input and output:

Enter a word: as you wish
aaaaaaaaasssssss   yyoou    wwwwwwwiishhhhh

To complete this code, you need to write two functions:

  • get_repeats(word) — Takes a word and returns a list of random numbers. There is one random number for each character in the word. For example, get_repeats('wow') might return [2, 4, 3].

  • do_repeats(word, repeats) — Takes a word and a list of numbers and returns a new word that repeats each character in the word based on its corresponding number in the list. For example, do_repeats(‘wow’, [2, 4, 3]) will return ‘wwoooowww’.

Discuss with the TA:

  • How did you implement these functions? Show a solution and discuss.
  • Is there anything you don’t understand about zip?
  • Make sure everyone understands every line of code we supplied.

Grading

To finish this lab and receive a grade, take the canvas quiz.

Solution

We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)