Python for Asshats

Chapter 4

Currently your programs can only do the same things in the same order, which is pretty nice. But anyone who's played Fognite knows that programs have the ability to do lots of different things and respond to user input. That's why this chapter introduces various control statements that allow you to add spice to your program.

The old in-and-out

I would like to introduce you to your first bit of program interactivity: input! input is a function. It takes one parameter, the prompt, which it displays onscreen. It then pauses the execution of the program until you type something in the console and hit enter. It then returns the value of what you typed as a string, and continues excecution. It can be a bit janky sometimes, so make sure your cursor is right after the last bit of text displayed.

Code:
you_said = input("Say something: ")  # read user input to variable
print("You said: " + you_said)  # output it back
Possible output:
Say something: i can put whatever i want here!
You said: i can put whatever i want here!

This is the simplest kind of user input, and is especially handy if you want to test your code on a whole bunch of test cases. input always gives you a string, so let's try doing some string things with it.

Code:
word = input("What is your favorite word? ")  # user input is a string
print("cum" in word)  # test user input for "cum" (remember the in keyword?)
Possible output:
What is your favorite word? cucumber
True

Now run the same program again, but give it a different input.

Possible output:
what is your favorite word? jar
False

Because of type conversion, you can also do not string things using your inputs. Take this basic program for dividing two numbers:

Code:
# take input and immediately convert
num_1 = float(input("Dividend: "))
num_2 = float(input("Divisor: "))  
quotient = num_1 / num_2
# i had to google those words because I haven't used them since primary school
print("Result is " + str(quotient))
Possible output:
Dividend: 420
Divisor: 69
Result is 6.086956521739131

Be carefull, however. Remember the type conversion rules from the previous chapter, the program will shit itself and die if you enter anything besides a number.

Possible output:
Dividend: rawr x3
Traceback (most recent call last):
  File "/home/timgor/test.py", line 2, in <module>
    num_1 = float(input("Dividend: "))
ValueError: could not convert string to float: 'rawr x3'

Later on, we will cover how to recover elegantly from malformed input and adequately scold the user.

Consider yourself blocked

Python code is seperated into "blocks". No, not like Minecraft, you dipshit. These are portions of the code that the interpreter runs as a single unit. All of your programs have been in a single block, so this seperation seems a bit pointless, but it comes into play when you want to control the flow of the program. A code block is denoted by a section of code indented to same level (tabbed over the same number of times). The simplest use of code blocks is with the if bool: statement. You provide the if statement with a boolean and it runs the following block of code only if that boolean evalutes to True.