GAMR1520: Markup languages and scripting

python logo

Lab 1.3: Problem solving

Part of Week 1: Introducing python

General setup

For all lab exercises you should create a folder for the lab somewhere sensible.

Assuming you have a GAMR1520-labs folder, you should create a GAMR1520-labs/week_1 folder for this week and a GAMR1520-labs/week_1/lab_1.3 folder inside that.

Create a collection of scripts. If necessary, use folders to group related examples.

GAMR1520-labs
└─ week_1
    └─ lab_1.3
        ├─ experiment1.py
        └─ experiment2.py

Try to name your files better than this, the filename should reflect their content. For example, string_methods_.py, conditionals.py or while_loops.py.

Make sure your filenames give clues about what is inside each file. A future version of you will be looking back at these, trying to remember where a particular example was.

General approach

As you encounter new concepts, try to create examples for yourself that prove you understand what is going on. Try to break stuff, its a good way to learn. But always save a working version.

Modifying the example code is a good start, but try to write your own programmes from scratch, based on the example code. They might start very simple, but over the weeks you can develop them into more complex programmes.

Think of a programme you would like to write (don't be too ambitious). Break the problem down into small pieces and spend some time each session trying to solve a small problem.

Now we have a basic understanding of data types and looping, we can start creating our own code. Spend some time completing these coding challenges.

If you find them easy, then ask for something harder.

If you find them difficult, ask the people around you and your tutor for some clues.

A guessing game

OK, we’ve been through some of the very basic elements of programming. Now Try to do something difficult.

Write a programme which generates a random integer and asks the user to guess it. The output might look something like this:

====================
Guessing game
====================

Guess the number:
[1-100] >> 50

It's lower than 50:
[1-100] >> 25

It's higher than 25:
[1-100] >> 37

It's lower than 37:
[1-100] >> 31

It's higher than 31:
[1-100] >> 34

It's higher than 34:
[1-100] >> 36

It's lower than 36:
[1-100] >> 35
Well done!
The number was 35!

The numbers after >> prompts are user input.

This is a non-trivial piece of code. Don’t worry about getting it perfect at first. Here’s a few ideas for how to start.

  • put the line from random import randint at the top of your code.
  • Use randint() to produce a random number and store it in a variable
  • Print your number out at first, while you test the code.
  • Print out a welcome message, formatted in the way you want it.
  • Ask the user for a guess using a suitable prompt.
  • Inform the user whether they got it, were high, or low.
  • Implement a loop which breaks when the guess is correct.

If you want to extend this further, try adding a feature which allows the user to play again.

A number guesser

Write a programme that performs the opposite role. In this case the player will guess a number and the programme will ask questions and try to guess.

===================================
Think of a number between 1 and 100
          (Don't tell me)           
===================================
Press ENTER when you are ready.

Is it 50?
 [y/n] >> n

Is it greater than 50?
 [y/n] >> n

Is it 25?
 [y/n] >> n

Is it greater than 25?
 [y/n] >> y

Is it 37?
 [y/n] >> n

Is it greater than 37?
 [y/n] >> n

Is it 31?
 [y/n] >> n

Is it greater than 31?
 [y/n] >> y

Is it 34?
 [y/n] >> y

I got it in 5 guesses!

[50, 25, 37, 31, 34]

Hint: The maximum and minimum possible values will start at 1 and 100. Use these numbers to produce a guess and when the player answers, update them accordingly.

Make sure you complete these before next week.

If you have extra time then write your own text-based system to do something similar. Or try these ideas.