Video Coming Soon...

Created by Zed A. Shaw Updated 2024-11-15 13:50:32
 

14: Prompting and Passing

Let's do an exercise that uses argv and input together to ask the user something specific. You will need this for the next exercise where you learn to read and write files. In this exercise we'll use input slightly differently by having it print a simple > prompt.

View Source file ex14.py Only

from sys import argv

script, user_name = argv
prompt = '> '

print(f"Hi {user_name}, I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)

print(f"Where do you live {user_name}?")
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}.  Not sure where that is.
And you have a {computer} computer.  Nice.
""")

We make a variable prompt that is set to the prompt we want, and we give that to input instead of typing it over and over. Now if we want to make the prompt something else, we just change it in this one spot and rerun the script. Very handy.

Remember that you have to do the same thing you did in Exercise 13 and use the terminal to make this work. This will be the last time for a while, but it's important to know how to run code from the Terminal since that's a very common way to run Python code.
Previous Lesson Next Lesson

Register for Learn Python the Hard Way, 5th Edition (2023-2024)

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.