Lesson 1

Python

Starter

  1. Make a Code Club folder in your area.
  2. Download the easygui library into your folder. Unzip it so you have an easygui folder.
  3. Make a new Python script with the following:
import easygui

if easygui.ynbox('Are you a Cat Person?', 'Cats or Dogs?', ('Yes', 'No')):
    print("You are a Cat Person.")
else:
    print("You are a Dog Person.")

Extension: Play around with the different inputs for easygui.ynbox

https://malverncode.club/

Asking a Simple Question

You can ask the user a simple question by:

import easygui

result = easygui.msgbox("Pleased to meet you!", "Welcome")

if result == "OK":
    print("You pressed OK")

Multiple Choice

import easygui

result = easygui.buttonbox("Which flavor Ice Cream do you prefer?", "Favourite Flavor",
                           ("Chocolate", "Vanilla", "Strawberry"))

print("I like " + result + " Ice Cream too.")

The result variable stores the output of the function easygui.buttonbox. It will be one of the options specified: Chocolate, Vanilla or Strawberry.

 

Extension: Try changing some of the inputs; change the question, add more options...

Password

import easygui

result = easygui.passwordbox("Please enter your password", "Password")

print("Your password is " + result + ".")

Multiple Choice Box

This shows the user a list of items:

import easygui

result = easygui.choicebox("What would you like to buy", "Supermart", 
                           ["Milk", "Eggs", "Cheese", "Butter"])

print("Here is your " + result + ". Please come again!")

Now try and use this code with some of the other code you have to try and build a very simple shop.

Reference

easygui.msgbox(message, title)
# returns "OK" when the OK button is pressed.

easygui.buttonbox(message, title, ["item1", "item2", "..."])
# returns one of the items in the array

easygui.choicebox(message, title, ["item1", "item2", "..."])
# returns one of the items in the array

Look at some of the more examples here: http://vh7.uk/vd

Next Time

APIs

Code Club: Python 1

By Jake Walker

Code Club: Python 1

  • 571