Introduction to Python
  • Introduction
  • Preface
  • Background
  • Installing Python
  • Getting Started
  • Basics
    • More Printing
    • Strings
    • Numbers
    • Exercises
    • Comments
  • Variables
    • Operators
    • Type Conversion
    • Exercises
    • A Brief Introduction to Lists
    • Game Exercise
  • Human Input
  • Functions
    • First Functions
    • Why Functions
    • Exercises
  • Indentation
  • Decisions
    • Booleans
    • Logical Operators
    • If Statements
    • Elif and Else Statements
    • Exercises
    • Rock, Paper, Scissors
    • Game Exercise
    • Game Exercise 2
  • Lists
  • Loops
    • For Loops
    • While Loops
  • More Data Structures
    • Tuples
    • Dictionaries
  • Pygame
  • Extra Content
    • Computers and Code
    • More About Python
    • For Loops with Range
    • List Slicing
Powered by GitBook
On this page

Loops

With loops we'll finally have all the Pythonic ingredients to begin making games. Loops allow us to repeat tasks over and over again. Think about how we used if statements before to check user input:

def is_valid_user(username):
    if username == 'shaka zulu':
        return True
    else:
        print('Invalid username')
        return False

Well if our entire program checked the input once, it would end with False if I entered 'eric williams'. What if we wanted to give the user 3 times before we stop them from trying? Loops can be useful then.

PreviousListsNextFor Loops

Last updated 6 years ago