This repository was archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.py
More file actions
200 lines (162 loc) · 7.53 KB
/
task3.py
File metadata and controls
200 lines (162 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# # This program is free software: you can redistribute it and/or modify
# # it under the terms of the GNU General Public License as published by
# # the Free Software Foundation, either version 3 of the License, or
# # (at your option) any later version.
# # This program is distributed in the hope that it will be useful,
# # but WITHOUT ANY WARRANTY; without even the implied warranty of
# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# # GNU General Public License for more details.
# # You should have received a copy of the GNU General Public License
# # along with this program. If not, see <https://www.gnu.org/licenses/>.
# # 2019 May/June Prerelease Material.
# # Solved by Sadman Tariq
# # https://github.com/SadmanTariq/2019PrereleaseWithSolution
print("This solution is created by Sadman Tariq.")
print("https://github.com/SadmanTariq/2019PrereleaseWithSolution \n")
# # This program is free software: you can redistribute it and/or modify
# # it under the terms of the GNU General Public License as published by
# # the Free Software Foundation, either version 3 of the License, or
# # (at your option) any later version.
# # This program is distributed in the hope that it will be useful,
# # but WITHOUT ANY WARRANTY; without even the implied warranty of
# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# # GNU General Public License for more details.
# # You should have received a copy of the GNU General Public License
# # along with this program. If not, see <https://www.gnu.org/licenses/>.
# # 2019 May/June Prerelease Material.
# # Solved by Sadman Tariq
# # https://github.com/SadmanTariq/2019PrereleaseWithSolution
print("This solution is created by Sadman Tariq.")
print("https://github.com/SadmanTariq/2019PrereleaseWithSolution \n")
# ------TASK 1------
less_than_ten = True
while less_than_ten:
try:
numItems = int(input("Enter amount of items to be put on aution " +
"(atleast 10): "))
if numItems >= 10:
less_than_ten = False
except ValueError:
print("\nCan only be numbers.")
# Lists containing different properties of auction items.
# Dictionary would have worked better but O' Level restrictions.
ItmNumList = []
ItmDescList = []
ReservePriceList = []
NumBidsList = [] # List containing number of bids for each item
BidList = [] # List containing highest bid for each item
BuyerNumList = [] # List containing buyer number of highest bidders
SoldList = [] # List containing whether each item is sold or not
# Loop for number of items times.
for i in range(numItems):
all_input_correct = False
while not all_input_correct:
num = input("Enter item number: ")
try:
# Check if input can be converted to integer.
# If it can't be converted then it contains non numbers.
int(num)
except ValueError:
print("Item number may only contain whole numbers.")
else:
# This part only executes if num contains only numbers.
if int(num) < 0:
# Negative numbers are not allowed.
print("Item number may only contain whole numbers.")
elif num in ItmNumList:
print("Item number needs to be unique.")
else:
ItmNumList.append(num)
all_input_correct = True
ItmDescList.append(input("Enter item description: "))
reserve_price_input = input("Enter reserve price: $")
is_number = False
# Defaults to false so that condition is checked at least once.
while not is_number:
# This part is checked repeatedly until input is valid.
try:
int(reserve_price_input)
except ValueError:
print("Reserve price may only be a positive whole number." +
" Try again.")
reserve_price_input = input("Enter reserve price: $")
else:
# is_number is set to True only when ValueError is not raised.
is_number = True
# The input does not immediately get added to the reserve prices list.
while reserve_price_input < 0:
print("Reserve price must be positive. Try again.")
reserve_price = int(input("Enter reserve price: $"))
ReservePriceList.append(reserve_price_input) # Add it after the checks.
NumBidsList.append(0)
BidList.append(0)
BuyerNumList.append("")
SoldList.append(False)
# ------TASK 2------
# Print all the available items for selection using Item Number.
print("Available items:")
for i in range(numItems):
print(ItmNumList[i], ItmDescList[i], sep=": ")
WantToBid = True # When false; break out of loop.
while WantToBid:
choice = input("Do you want to place a bid? (y/n): ")
# If the choice is 'n' then WantToBid is set to False and the elif
# segment does not run.
# If choice is 'y' then WantToBid is not modified and the elif segment
# is run.
# If choice is neither 'y' nor 'n' then nothing happens and the user is
# prompted again.
if choice == 'n':
WantToBid = False
elif choice == 'y':
SelectedItem = '' # Stores item number of selected item.
BidAmount = 0
BuyerNumber = ''
item_num_correct = False # True if selected item is available.
while not item_num_correct:
SelectedItem = input("Enter item number from above: ")
if SelectedItem in ItmNumList:
# This segment only executes if the selected item number
# exists in ItmNumList.
item_num_correct = True
list_index = ItmNumList.index(SelectedItem)
print() # Blank line
print(SelectedItem, ItmDescList[list_index])
print("Highest bid: $" + str(BidList[list_index]))
else:
print("Invalid item number; try again.")
bid_correct = False # True if bid is higher than current highest.
while not bid_correct:
BidAmount = int(input("Enter your bid: $"))
if BidAmount > BidList[ItmNumList.index(SelectedItem)]:
bid_correct = True
else:
print("Bid amount must be higher than previous bid.")
BuyerNumber = input("Enter buyer number: ")
list_index = ItmNumList.index(SelectedItem) # Index of item.
BidList[list_index] = BidAmount
BuyerNumList[list_index] = BuyerNumber
NumBidsList[list_index] += 1
# ------TASK 3------
TotalFee = 0.0 # 0.0 instead of 0 because it needs to be float.
LessThanReservePrice = [] # Items with highest bid lower than reserve.
NoBids = [] # Items with no bids.
for i in range(numItems):
if BidList[i] >= ReservePriceList[i]: # Sold?
SoldList[i] = True
TotalFee += BidList[i] * 0.1 # Fee is 10% of bid.
else:
LessThanReservePrice.append(ItmNumList[i])
if NumBidsList[i] == 0: # No bids?
NoBids.append(ItmNumList[i])
# Printing information.
print("\n------------------------")
print("Total fee: " + str(TotalFee))
print("Number of items sold: " + str(numItems - len(LessThanReservePrice)))
print("\nThe {0} items that have not".format(len(LessThanReservePrice)),
"reached their reserved price are:")
for x in LessThanReservePrice:
print(x, " Highest bid: $", BidList[ItmNumList.index(x)], sep='')
print("\nThe {0} items that have recieved no bids are:".format(len(NoBids)))
print(', '.join(NoBids)) # Print NoBids delimited with ', '
input() # Wait before exiting.