User Input¶
IPO Model¶
Nearly every software application follows the Input, Processing, Output (IPO) Model. So far our programs have only covered processing (variable assignment, calculations, the creation of strings) and output (printing of strings to the terminal). From now on we will be writing programs that incorporate user input as well.
In Python this is accomplished using the input()
function. When you run the program, it will actually pause and wait for you to type something in.
Manually write out the code below and run it. Name the file:
user_input.py
print("Enter the following information about an item you wish to purchase..")
print()
print("The name of the item:")
name = input()
price = float(input("The price: $"))
print("How many do you want?")
quantity = int(input())
subtotal = price * quantity
tax = subtotal * 0.13
total = subtotal + tax
print()
print(f"You choose to buy {quantity} {name}.")
print(f"That will come out to ${total}")
What You Should See¶
Enter the following information about an item you wish to purchase..
The name of the item:
Apples
The price: $0.50
How many do you want?
5
You choose to buy 5 Apples.
That will come out to $2.825
What You Should Do on Your Own¶
Assignments turned in without these things will not receive any points.
What is different about how we took user input concerning the
price
as compared to how input was taken for the itemname
? There are two differences.Because it looks better, make your user input for
name
andquantity
also appear on the same line (likeprice
).What is a prompt? Why does switching the order as shown below cause a usability issue?
name = input() print("Enter the name of the item:")
Explain the use of the
int()
andfloat()
functions. Why are they used? What happens if you remove them? i.e.,price = input("The price: $")
©2021 Daniel Gallo
This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.