Dr. Cho’s Website
Course Materials

Python exercises

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

1   Arithmetic calculator

Keywords: keyboard input, looping, slicing, branching

Write a simple calculator that supports four basic arithmetic operations (+, -, *, and /) for two floating-point numbers.

  1. Print “? ” and read an expression
  2. If the expression is quit, stop
  3. Split the expression into three parts: left operand, operator, and right operand
  4. Branch based on the operator
  5. Calculate the expression and print its result
  6. Go to step 1

2   Dice rolling

Keywords: keyboard input, probability, random()

Python provides a pseudo-random number generator in the random module. We’ll use the random() function from this module to write a dice rolling simulator. This function takes no arguments and returns a pseudo-random float between $[0,1)$. That is, $0\le\text{random()}<1$. The workflow of this program should be as follows:

  1. Print “Roll? ” and read a string input
  2. If the input is quit, stop
  3. Take a random number from random()
  4. Print one of 1–6 based on the random number from step 3
  5. Go to step 1

Do NOT use randint(). Only use these three functions: random(), print(), and input().

3   HTTP client

Keywords: http, http.client, open

Write a simple HTTP client that downloads https://hcho.isnew.info/gisc-3200k.html and saves it as test.html.