Exilian

Game Design and Project Resources: The Workshops Quarter => Computer Game Development - The Indie Alley => Game & program tutorials => Topic started by: Son of the King on January 30, 2012, 11:28:29 PM

Title: SotK's Python Tutorials - Part 5: User Input
Post by: Son of the King on January 30, 2012, 11:28:29 PM
SotK's Python Tutorials
Part 5: User Input



User Input:

In the last tutorial, we used the input() function for the first time. Now we will explore this in more detail.

Imagine we want to make a program that we can tell our name and it will then say hello to us. Think of it as an extension of the Hello World program from earlier.

First we will need to ask the user to input their name. We do this by using the input() function, with something like 'What is your name?' as the prompt:

Code: (Input) [Select]
name = input('Please enter your name: ')
Now we need to print out the name that the user entered.

Code: (Output) [Select]
print(name)
We don't need quotes because name is a variable. Putting this together, we get:

Code: (Hello <name>!) [Select]
# Get name
name = input('Please enter your name: ')

# Print a welcome message
print('Hello, ' + name + '!')

How about if we want to input a number. That is apparently easy enough as we saw in the last tutorial.

Code: (Input a number) [Select]
num = int(input('Enter a number: '))
How about if we want to use that number in a string, e.g. asking a person their age and repeating it (a possible extension of our hello program).

Code: [Select]
name = input('Please enter your name: ')
age = int(input('Please enter your age: '))

print('Hello. Your name is ' + name + '. You are ' + age + ' years old.')

This code gives an error! This is because we tried to add an integer (age) to a string. In the case where you don't need the number you input to be an integer (as in this case), you don't need the int() around the input().

We now have all the tools needed to make a command-based program (indeed you could make a text RPG now :P ). Lets make a simple calculator that runs on the command line!

Breaking down the problem:

We know we want to make a calculator, but where to start? We split down the program into different parts, for example we want to be able to:


We want to be able to do all of those things an unlimited amount of times. This means that the first thing we need is a loop:

Code: (Main loop) [Select]
running = True
while running:
    # Calculator stuff here

At the moment, that creates an infinite loop, so we need a way of breaking out of it.

Code: (Main loop) [Select]
running = True
while running:
    command = input('Enter a command: ')
    if command == 'quit':
        running = False
    # Calculator stuff here

Now we will be prompted to enter a command until we type quit. Lets think how we add two numbers together!

Code: (Adding) [Select]
first = int(input('Enter first number: '))
second = int(input('Enter second number: '))

print(first + second)

Now lets combine this with our loop.

Code: (Start of the calculator!) [Select]
running = True
while running:
    command = input('Enter a command: ')
    if command == 'quit':
        running = False
    elif command == 'add':
        first = int(input('Enter first number: '))
        second = int(input('Enter second number: '))

        print(first + second)

We can do the other 3 operators similarly, giving us the following.

Code: (Calculator!) [Select]
print('This is a calculator!\n\n')
running = True
while running:
    command = input('Enter a command: ')
    if command == 'quit':
        running = False
    elif command == 'add':
        first = int(input('Enter first number: '))
        second = int(input('Enter second number: '))

        print(first + second)
    elif command == 'subtract':
        first = int(input('Enter first number: '))
        second = int(input('Enter second number: '))

        print(first - second)
    elif command == 'multiply':
        first = int(input('Enter first number: '))
        second = int(input('Enter second number: '))

        print(first * second)
    elif command == 'divide':
        first = int(input('Enter first number: '))
        second = int(input('Enter second number: '))

        print(first / second)

While this is pretty nice, it would be better if we could have a graphical interface like most people are used to. That's for next time!