Date

October 13, 2016

Activity 1: Login and Record Attendance

We will be taking roll in lab each week! Please come to your assigned lab to be counted present!

If you are in an Olsson lab, click "Lab Attendance" on the left-hand menu in Collab to register your attendance and keep up with your lab grade.
YOU MUST CLICK THE LARGE GREEN OR YELLOW BUTTON FOR YOUR ATTENDANCE TO COUNT!!

You must do this from a machine in Olsson 001 and not your laptop. If you have trouble, talk to your lab TA. Students in Lab 109 will do attendance via direction from the TA.

Activity 2: Starting Out

The TAs will review how functions work and why we want to use them. They will also go through a few basic examples of writing methods.

Then find someone to work with for this week!

Activity 3: Setup

If you've never used UrbanSpoon, it's an app that helps you choose a restaurant to go to based on some choices and a whole lot of randomness. We're going to build a simplified version of this app using functions.

Create a file called wahoospoon.py. Start off by setting up some variables to store restaurant names, styles, and costs. Our suggestion is to use three lists in which the indexes of each restaurant lines up with its style and cost.

import random

restaurants = ["Sticks", "Yuan Ho", "Melting Pot", "East Garden"]
styles = ["Casual", "Chinese", "Fancy", "Chinese"]
costs = ["$", "$", "$$$", "$$"]

Of course, add your own entries here!

Activity 4: Prompt The User and Get Lunch!

Write code to choose between three options: get a random restaurant, get a random restaurant based on style, and get a random restaurant based on cost.

It should look something like this:

Welcome to WahooSpoon!
1. Get a random restaurant
2. Get a random restaurant based on style
3. Get a random restaurant based on cost
Choice?:

If the user chooses 1, you should call a function called get_random_restaurant() to get a random restaurant. The function should return three strings: the name of the restaurant, the style, and the cost.

If the user chooses 2, you should print the list of available styles, removing any duplicates from this printed list. Prompt the user for which style the user wants. Then call a function called get_restaurant_style(chosen_style) to get a random restaurant. The function should only take the style the user types as a parameter. The function should return three strings: the name of the restaurant, the style, and the cost.

If the user chooses 3, you should print the list of available costs, removing any duplicates from this printed list. Prompt the user for which cost the user wants. Then call a function called get_restaurant_cost(chosen_cost) to get a random restaurant. The function should only take the cost the user types as a parameter. The function should return three strings: the name of the restaurant, the style, and the cost.

An example run could look like this:

Welcome to WahooSpoon!
1. Get a random restaurant
2. Get a random restaurant based on style
3. Get a random restaurant based on cost
Choice?: 2
{'Fancy', 'Chinese', 'Casual'}
What style would you like?: Chinese
We're going to East Garden today! (Style: Chinese / Cost: $$ )

Code To Start With

To help you out, here is some code to start with, including the headers for the functions you need to write. At no time should you hardcode anything from the list of restaurants, styles, or costs! Imagine how the program should run if we added all the places in Charlottesville.

import random

restaurants = ["Sticks", "Yuan Ho", "Melting Pot", "East Garden"]
styles = ["Casual", "Chinese", "Fancy", "Chinese"]
costs = ["$", "$", "$$$", "$$"]

def get_random_restaurant():
    # Write your code here!

def get_restaurant_style(chosen_style):
    # Write your code here!

def get_restaurant_cost(chosen_cost):
    # Write your code here!


print("Welcome to WahooSpoon!")
print("1. Get a random restaurant")
print("2. Get a random restaurant based on style")
print("3. Get a random restaurant based on cost")
choice = int(input("Choice?: "))
if choice == 1:
    r, s, c = get_random_restaurant()
elif choice == 2:
    print(set(styles))
    style = input("What style would you like?: ")
    r, s, c = get_restaurant_style(style)
else:
    print(set(costs))
    cost = input("What cost would you like?: ")
    r, s, c = get_restaurant_cost(cost)

print("We're going to", r, "today! (Style:", s, "/ Cost:", c, ")")

Submission: Each partner should submit one .py file named wahoospoon.py to the submission system at https://archimedes.cs.virginia.edu/cs1110/. Please put both partners' names and id's in two comments at the top of the file.

You must submit on time! Even if you don't finish, submit what you have.

Solution Code

{% codeblock lang:python %}

Mark Sherriff (mss2x)

import random

restaurants = ["Sticks", "Yuan Ho", "Melting Pot", "East Garden"] styles = ["Casual", "Chinese", "Fancy", "Chinese"] costs = ["$", "$", "$$$", "$$"]

def get_random_restaurant(): i = random.randint(0,len(restaurants)-1) return restaurants[i], styles[i], costs[i]

def get_restaurant_style(chosen_style): random_style = "" while random_style != chosen_style: i = random.randint(0,len(restaurants)-1) random_style = styles[i]

return restaurants[i], styles[i], costs[i]

def get_restaurant_cost(chosen_cost): random_cost = "" while random_cost != chosen_cost: i = random.randint(0,len(restaurants)-1) random_cost = costs[i]

return restaurants[i], styles[i], costs[i]

print("Welcome to WahooSpoon!") print("1. Get a random restaurant") print("2. Get a random restaurant based on style") print("3. Get a random restaurant based on cost") choice = int(input("Choice?: ")) if choice == 1: r, s, c = get_random_restaurant() elif choice == 2: print(set(styles)) style = input("What style would you like?: ") r, s, c = get_restaurant_style(style) else: print(set(costs)) cost = input("What cost would you like?: ") r, s, c = get_restaurant_cost(cost)

print("We're going to", r, "today! (Style:", s, "/ Cost:", c, ")")