Asking and converting
Make your script ask the user for data and understand why "5" is not the same as 5.
Why this matters for your mission
A script that downloads pages almost always needs data that changes: which site to visit, how many pages to request.
Today you learn to receive them with input() and to avoid the most common mistake for beginners: mixing text with numbers.
Before moving on: recall lesson 1
Try to answer without going back.
What does this program show? Type exactly what appears on screen.
site = "python.org"
print("https://" + site)
The + joins the two texts, without adding spaces.
Four ideas
1. input() asks and returns what the user types
What the person types always arrives as text, even if it's digits.1
domain = input("Domain (e.g. python.org): ")
print("I'm going to visit https://" + domain)
2. Values have a type, and type() tells you which
print(type("5")) # <class 'str'> text
print(type(5)) # <class 'int'> integer
print(type(2.5)) # <class 'float'> decimal
The type changes what operators do. With numbers, * multiplies; with text, it repeats:1
print(5 * 2) # 10
print("5" * 2) # 55
3. int() and str() convert between types
int("5") turns the text into the number 5. str(5) does the opposite.1
pages = int(input("How many pages? "))
print(pages * 2) # if you type 5: 10
If the text doesn't look like a number, the conversion fails:
int("abc")
ValueError: invalid literal for int() with base 10: 'abc'
4. Joining text with a number gives an error, and you fix it by converting
print("Total: " + 10)
TypeError: can only concatenate str (not "int") to str
print("Total: " + str(10))
Total: 10
Python reads the message like this: "I can only join text with text, and you gave me an integer". There's also len(), which counts the characters in a text: len("https://python.org") gives 18.1
Do it yourself
- In the
practicefolder, createvisit.pywith this content (type it, don't paste it; typing makes it stick better):domain = input("Domain (e.g. python.org): ") pages = int(input("How many pages do you want to download? ")) seconds = pages * 2 print("I'm going to visit https://" + domain) print("With a 2-second pause per page it will take me", seconds, "seconds") print("The address has", len("https://" + domain), "characters") - Run it with
python3 visit.pyand answerpython.organd5. You should see10 secondsand18 characters. - Break it. Change the
pagesline topages = input("How many pages do you want to download? ")(withoutint), run it with5and watch what happens withseconds. Then restoreint(...). - A challenge: add a line that shows the time in minutes using
/ 60(Python's division). What type of number does it return?
The 2-second pause is a habit that will matter a lot when we request real pages: later we'll see why it's not a good idea to request them at full speed.
Check what you remember
The user types 5. What does this program show? Type exactly what appears.
pages = input("How many pages? ")
print(pages * 2)
input() returns text. Since pages is the text "5", multiplying it by 2 repeats it: 55. It's neither a sum nor a product.
This line gives TypeError: can only concatenate str (not "int") to str. Which one fixes it?
print("Total: " + 10)
To join with + both sides must be text, so the number is converted with str(). int(10) is still an integer.
The user types abc when the program asks for a number. Which line fails?
domain = input("Domain: ")
pages = int(input("Pages: "))
print("Done")
int("abc") raises ValueError because that text isn't a number. Later you'll learn to handle that case without the program crashing.
Main source
Finish Chapter 1: Python Basics of Automate the Boring Stuff with Python.
Today's sections are The Integer, Floating-Point, and String Data Types, String Concatenation and Replication and, within Dissecting the Program, the parts about input(), len(), str()/int()/float() and type().
Official equivalent: An Informal Introduction to Python.
Remember: your teacher is the agent you're talking to. If visit.py doesn't work, paste the code and the complete error. You can also ask things like "why does input() return text?" or "what else can I do with len()?".