Skip to content

Latest commit

 

History

History
55 lines (37 loc) · 1.79 KB

File metadata and controls

55 lines (37 loc) · 1.79 KB

Session 1 Problems

In session 1 we learned:

  1. How to import a module
  2. Flow control and conditionals
  3. How to use loops

Work through the following questions at your own pace. Don't forget to ask questions if you get stuck!

A little theory

  • What is the difference between the equal to operator == and the assignment operator =?
  • What is an infinite loop?
  • What is the difference between break and continue?
  • Which Python utility can you use to run a loop a certain amount of times?

Journey to work

How far do you have to travel to get to work? Ask the user how many kms they travel to work, then give them a suggestion based on their repsonse:

  • if the user travels greater than 5km to work, print 'You should take the train.'
  • if the user travels greater than 2km to work, print 'You should cycle.'
  • if the user travels 2km or less to work, print 'Stop being lazy and walk!'

Here are some example inputs and their expected outputs:

Input Output
6 'You should take the train.'
4 'You should cycle'
2 'Stop being lazy and walk!'

FizzBuzz

Iterate over each number from 1 to 100. For each number (n) in the range, do the following:

  • if n is divisible by 3, print 'Fizz' to the console
  • if n is divisible by 5, print 'Buzz' to the console
  • if n is divisible by 3 and 5, print 'FizzBuzz' to the console
  • if none of the above apply, just print n to the console

Double char

Ask the user to input a string. Return this string, with every character (char) repeated twice.

e.g. 'python' should become 'ppyytthhoonn'

Teacher's pet edition: Ask the user for an number (n) as well. Repeat each char in the given string n times.

Sum

Calculate the sum of:

  • every number from 1 to 100
  • every odd number from 1 to 100
  • every number from 1 to 100 divisible by 3