LPTHW - Exercise 11: Asking Questions?

LPTHW - Exercise 11

Yay! Some new stuff! I must admit, this first section is getting a little tedious so it's nice to have something new now.

It's a common thing with programming tutorials, they all start of with such simple things and take ages to get past them. I think it's been a factor in why it's taken me a while to really want to learn to code.

Thankfully, with this LPTHW format you can at least just copy down the boring lessons and get through it to the interesting concepts. Anywho, here we go then...

print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weigh?", end=' ')
weight = input()

print(f"So, you are {age} years old, {height} tall and weigh {weight}.")

Learn Python The Hard Way - Study Drills

1. Go online and find out what Python's input does.

Taken from here, input() is described as:

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example:

>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"

2. Can you find other ways to use it? Try some of the samples you find.

I found a slightly different way to use it and that's in the answer to the question below. I also saw it used like so:

input("Press <enter> to continue")

That's a useful way to indicate to a user something has completed and makes them feel like then are authorising continuation.

Another useful thing I saw was an example of how to convert the output, which is a string, to something else like an integer:

quantity = int(input("How many? "))

This is called 'type conversion' and looks like it would be pretty useful to turn the users input into the exact type of data you would need in your programme.

3. Write another "form" like this to ask some other questions.

# A new form for the study drill

name = input("What is your name? >>> ")
print(f'Hey {name}, let\'s be friends!')

Source files

As ever, source files on GitHub.

Spotted something wrong with this content? Tell me!

Get in touch on Twitter, GitLab or by email.