Student Summer Sprint
  • Welcome to Hackathons for Schools Student Summer Sprint!
  • Event Information
  • What is the Student Summer Sprint?
  • Useful Event Information and Links
  • Schedule
  • Our Organisers, Panelists and Mentors
    • Organisers
    • Panelists and Speakers
    • Mentors
  • Project Details
    • Themes
    • Judging Criteria and Winning Teams
    • Presentation Advice
    • Submission Checklist and FAQs
  • Coding Platform and GitLab
  • Technical Workshops
    • How to Use Git
      • Git and the Terminal
      • How to use Git - The Basics
      • Branching, Merging and Other Useful Commands
      • How to use Git - Further Resources
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • Web Development - Resources
    • Introduction to Python
      • Variables
      • Data Types and String Formatting
      • Input and Output
      • Conditional Statements
      • Functions
      • Libraries
      • CHALLENGES
        • Solutions
      • Learning Python - Resources
    • Discord Bots in Python
      • Discord Bots - Resources
    • Web Scraping in Python
  • Careers Advice and Opportunities
    • University Advice
      • University Advice - Further Resources
    • CV, Applications and Interviews
      • Creating a Great CV
      • UCAS Personal Statement Advice
      • Interview Hints and Tips
      • Ultimate LinkedIn Guide
      • Applications - Further Resources
    • Different routes into a tech career
      • Different Routes into Tech - Further Resources
    • What Now?
      • More Opportunities!
Powered by GitBook
On this page
  • Challenge 1: String Formatting
  • Solution
  • Challenge 2: Conditional Statements
  • Solution without extension
  • Solution with extension
  • Challenge 3: Using Libraries
  • Solution

Was this helpful?

  1. Technical Workshops
  2. Introduction to Python
  3. CHALLENGES

Solutions

Solutions to workshop challenges

Challenge 1: String Formatting

Solution

name = "Robert"
age = 19

print("Hi! My name is {} and I'm {} years old.").format(name, age)

Challenge 2: Conditional Statements

Solution without extension

print('''
Choose any of the three following ice cream flavours...
1. Chocolate
2. Strawberry
3. Vanilla

If you enter none of the above, you will be given mint.
''')

choice = int(input("Pick your choice (enter number 1-3) > "))

if (choice == 1):
    flavour = "chocolate"
elif (choice == 2):
    flavour = "strawberry"
elif (choice == 3):
    flavour = "vanilla"
else:
    flavour = "mint"

print("You chose {} flavoured ice cream.").format(flavour)

Solution with extension

print('''
Choose any two of the three following ice cream flavours...
1. Chocolate
2. Strawberry
3. Vanilla

If you enter only one or none, you will be given mint.
''')

options = ["chocolate", "strawberry", "vanilla"]

choices = input("Pick your choice (enter numbers 1-3) > ").split('&')

giveMint = False

if len(choices) < 2:
  giveMint = True

if giveMint:
  print("You chose mint flavoured ice cream.")
else:
  firstChoice = options[int(choices[0])-1] # must subtract 1 as indexing starts at 0
  secondChoice = options[int(choices[1])-1]
  print("You chose {} and {} flavoured ice cream.").format(firstChoice, secondChoice)

Challenge 3: Using Libraries

Solution

import turtle

t = turtle.Turtle()

def drawSquare():
    for i in range(4):
        t.forward(100)
        t.right(90)

def drawTriangle():
    for i in range(3):
        t.forward(100)
        t.right(120)

print('''
Choose any of the three following shapes to draw...
1. Square
2. Triangle
3. Circle

If you enter none of the above, a line will be drawn.
''')

choice = int(input("Pick your choice (enter number 1-3) > "))

if (choice == 1):
    drawSquare()
elif (choice == 2):
    drawTriangle()
elif (choice == 3):
    t.circle(50) # no need for a function - it's one line
else:
    t.forward(100) # same reasoning here - it's one line
PreviousCHALLENGESNextLearning Python - Resources

Last updated 4 years ago

Was this helpful?