Students build a movie-night ticket calculator with sliders, a dropdown and two outputs, learn that inputs match parameters and outputs match returned values in order, then add a popcorn checkbox, a seat radio and a calculator of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 10 min · Modify 15 min · Make 10 min
Read the unit primer first. return total, summary returns two values; Gradio puts them in the two outputs, in order (the Predict shows the same thing with a, b = ...). Each input is one parameter, in order. Kinds of value: Slider and Number give numbers (possibly 2.0, hence int()), Dropdown and Radio give the chosen text, Checkbox gives True or False, Textbox gives text. The bug task works out the summary but returns only the total, so one output stays empty. Encourage planning: inputs and outputs on paper before code.
Adding an input in the right place in both lists. Ask them to count inputs and parameters aloud.
| What you will see | A question to ask | The fix |
|---|---|---|
| An output box stays empty. | Does return have one value for each output? | return total, summary. |
| TypeError: price() takes 3 positional arguments but 4 were given. | How many inputs and how many parameters are there? | Add the parameter for the new input. |
| The summary says 2.0 adults. | What kind of number does the slider give? | str(int(adults)). |
One possible solution for each task. Other correct programs are fine.
import gradio as gr
def price(adults, children, day, popcorn):
if day == "Weekend":
total = adults * 18 + children * 12
else:
total = adults * 15 + children * 10
if popcorn:
total = total + 8
summary = str(int(adults)) + " adults and " + str(int(children)) + " children on a " + day.lower()
return total, summary
gr.Interface(
fn=price,
inputs=[
gr.Slider(0, 10, step=1, label="Adults"),
gr.Slider(0, 10, step=1, label="Children"),
gr.Dropdown(["Weekday", "Weekend"], value="Weekday", label="Day"),
gr.Checkbox(label="Popcorn ($8)"),
],
outputs=[gr.Number(label="Total ($)"), gr.Textbox(label="Summary")],
title="Movie night",
).launch()
import gradio as gr
def price(adults, children, day, seat):
if day == "Weekend":
total = adults * 18 + children * 12
else:
total = adults * 15 + children * 10
if seat == "Recliner":
total = total + (adults + children) * 5
summary = str(int(adults)) + " adults and " + str(int(children)) + " children on a " + day.lower()
return total, summary
gr.Interface(
fn=price,
inputs=[
gr.Slider(0, 10, step=1, label="Adults"),
gr.Slider(0, 10, step=1, label="Children"),
gr.Dropdown(["Weekday", "Weekend"], value="Weekday", label="Day"),
gr.Radio(["Standard", "Recliner"], value="Standard", label="Seat"),
],
outputs=[gr.Number(label="Total ($)"), gr.Textbox(label="Summary")],
title="Movie night",
).launch()
import gradio as gr
def pizza(people, slices_each, size):
if size == "Large":
per_pizza = 10
else:
per_pizza = 8
slices = people * slices_each
pizzas = int(slices // per_pizza)
if slices % per_pizza > 0:
pizzas = pizzas + 1
return pizzas, str(int(slices)) + " slices needed"
print(pizza(12, 3, "Large"))
gr.Interface(
fn=pizza,
inputs=[
gr.Slider(1, 40, step=1, label="People"),
gr.Slider(1, 6, step=1, label="Slices each"),
gr.Radio(["Medium", "Large"], value="Large", label="Size"),
],
outputs=[gr.Number(label="Pizzas to order"), gr.Textbox(label="Working")],
title="Pizza planner",
).launch()
Stretch: Add a Number input for a discount code percentage, and show the saving as a third output.
Support: Give students the popcorn checkbox line and the if, and ask them to put each in the right place.
A function has three parameters. How many inputs does its Gradio app need, and in what order?
Three, in the same order as the parameters.
Next lesson builds a quiz whose inputs come from a list of questions.