multiplying a string
what does the following code do?
"hello " * 3
can you use this idea to print a triangle of "*"
using just a single for loop?
controlling precision with str.format()
can you print the value 10/3, with only 2 digits of precision?
- hint: use the .format() function
CSV
here is some data in CSV (Comma Seperated Values) format:
name, age, country
avi cohen, 30, israel
ben hur, 25, united kingdom
can davidovich, 40, georgia
david ben shalem, 60, united states of america
efrat ben dagan levi, 25, united arab emirates
- print only the names of the people in this data, and nothing else
- note: don’t print commas
','
- hint: use
splitlines()
andsplit()
- note: don’t print commas
-
print only the ages
-
print only country names
-
print only the last 3 lines (hint: use splicing)
-
print the data without the header (hint: use splicing)
-
print every second line, without the header (hint: use splicing)
- print a smaller csv that has just ages and countries, without the header and without names
printing receipts
lets print a simple receipt using the .format() function the receipt will have 4 lines, looking like this:
cost : 362.50 NIS
vat : 17.00 %
vat : 61.62 NIS
total : 424.12 NIS
to determine the cost and the vat, lets use random numbers. use the following code:
import random
cost_nis = round(random.normalvariate(200, 50), 2)
vat_percent = random.randint(160, 180) / 10
# print(cost_nis, vat_percent)
compute the vat in NIS and the total and print the receipt
- NOTE: make sure that the numbers in the receipt are nicely aligned to make it readble!
- HINT: use the
str.format()
function, read the specification or go through some examples to get the hang of this mini-langauge
**
a full receipt
given a menu with prices that can look like this:
prices = {
"steak" : 150,
"omelet" : 25,
"coca cola" : 12,
"orange juice" : 16,
"pizza" : 49,
"hamburger" : 59,
"pita" : 1.5,
"tomato salad" : 13.5
}
and an a list containing an order from a table, such as:
order = ["steak", "steak", "pita", "pizza", "coca cola"]
use a for loop to print a full receipt
steak : 150.00 NIS
steak : 150.00 NIS
pita : 1.50 NIS
pizza : 49.00 NIS
coca cola : 12.00 NIS
----------------------------
cost : 362.50 NIS
vat : 17.00 %
vat : 61.62 NIS
total : 424.12 NIS
### useful: data
prices = {
"steak" : 150,
"omelet" : 25,
"coca cola" : 12,
"orange juice" : 16,
"pizza" : 49,
"hamburger" : 59,
"pita" : 1.5,
"tomato salad" : 13.5
}
order = ["steak", "steak", "pita", "pizza", "coca cola"]