Commit 653d1a72 by likorn

meow

parent 4242500a
Showing with 18 additions and 10 deletions
def input_number(text): def read_float(text):
n = input(text) n = input(text)
try: try:
float(n) float(n)
except ValueError: except ValueError:
return input_number("The value you've just entered is the wrong type. Try again!\n") return read_float("The value you've just entered is the wrong type. Try again!\n")
return float(n) return float(n)
def read_number(text):
n = input(text)
try:
int(n)
except ValueError:
return read_number("The value you've just entered is the wrong type. Try again!\n")
n = int(n)
if n < 1 or n > 15:
return read_number("The number should be between 1 and 15. Try again.\n")
return n
def read_array(n): def read_array(n):
tv = [] tv = []
for counter in range(n): for counter in range(n):
manufacture = input("The manufacture is ") manufacture = input("The manufacture is ")
price = input_number("And the price is ") price = read_float("And the price is ")
tv.append({'manufacture': manufacture, 'price': price}) tv.append({'manufacture': manufacture, 'price': price})
return tv return tv
def read_discount(text): def read_discount(text):
d = input_number(text) d = read_float(text)
if d < 10 or d > 60: if d < 10 or d > 60:
return read_discount("The discount can't be less than 10 or greater than 60. Try another\n") return read_discount("The discount can't be less than 10 or greater than 60. Try another\n")
return d return d
...@@ -27,20 +38,18 @@ def set_discount(tv, d): ...@@ -27,20 +38,18 @@ def set_discount(tv, d):
for counter in range(0, len(tv)): for counter in range(0, len(tv)):
current = tv[counter] current = tv[counter]
new_price = current['price'] * (1.0 - (d / 100.0)) new_price = current['price'] * (1.0 - (d / 100.0))
print("d: ", d, "old price: ", current['price'])
tv[counter]['new_price'] = new_price tv[counter]['new_price'] = new_price
return tv return tv
def display(tv): def display(tv):
print('%-16s%-12s%-12s' % ("Manufacture", "Price", "New price")) print('\n%-16s%-12s%-12s' % ("Manufacture", "Price", "New price"))
for each in tv: for each in tv:
print('%-16s%-12f%-12f' % (each['manufacture'], each['price'], each['new_price'])) print('%-16s%-12.2f%-12.2f' % (each['manufacture'], each['price'], each['new_price']))
number = int(input_number("Hey, how many TV sets do you want to compare?\n")) number = read_number("Hey, how many TV sets do you want to compare?\n")
tv_sets = read_array(number) tv_sets = read_array(number)
print(tv_sets)
discount = read_discount("Which discount do you want to get (in percents)?\n") discount = read_discount("Which discount do you want to get (in percents)?\n")
tv_sets = set_discount(tv_sets, discount) tv_sets = set_discount(tv_sets, discount)
display(tv_sets) display(tv_sets)
\ No newline at end of file
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment