So I have been trying to do this for a while now and am constantly coming up with differing failures. I need to take numerical input from the user and put it into a list and output it in decreasing value:
bids = []
bid = input('Bid: ')
while bid != '':
bids.append(bid)
bid = input('Bid: ')
print('The auction has finished! The bids were:')
for bid in bids:
bid = int(bid)
for bid in reversed(bids):
print(bid)
So this worked well most of the time, (I have been using the numbers 2,3,4 & 10 as input as I have been having problems where it shows the numbers in decreasing order for the first numeral) but when I type in 16, 30, 24 it displays the numbers as:
The auction has finished! The bids were:
24
30
16
Here is another version I have attempted:
bids = []
bid = input('Bid: ')
while bid != '':
bids.append(bid)
bid = input('Bid: ')
print('The auction has finished! The bids were:')
for bid in bids:
bid = int(bid)
bids.sort([::-1]) #Here is where the problem is at, I don't know
#what the correct syntax is for something like that
for bid in bids:
print(bid)
Any help will be much appreciated as I am fairly new to python and am struggling with my course.
-Callum
In your
bids.append(bid)
you get a list of strings. You want to convert them to integers and sort in decreasing order:
numerical_bids = [int(bid) for bid in bids]
numerical_bids.sort(reverse=True)
and now numerical_bids
is a list of integers sorted in decreasing order.
In your code:
for bid in bids:
bid = int(bid)
does nothing. It converts each bid
to an integer and forgets it immediately.
And
bids.sort([::-1])
is not how to use the sort
method. Read the docs for list.sort.
Your problem is fundamentally in this loop:
for bid in bids:
bid = int(bid)
The above does not alter the contents of bids
. It creates new integer instances, named bid
each time, which you don't do anything with. So when you later go to sort bids
, you are just sorting the original string elements, not your desired integers.
If you want to sort the integer values, you have a few choices. One is to convert the values to integers as they come in:
bids.append(int(bid))
One is to convert and sort right in your print loop:
for bid in sorted((int(x) for x in bids), reverse=True):
print(bid)
Since the approach you seemed to want to take is to replace the string elements of bids
with integer elements instead, you probably want to do something like your original loop, but don't throw away the results:
intbids = []
for bid in bids:
intbids.append(int(bid))
Lots of folks will tell you that the above is more concisely written as
intbids = [int(bid) for bid in bids]
I used intbids
as a separate list to make it clear what is happening. If you want to just throw away the string bid list once you have the integer bid list, you can just use the same name:
bids = [int(bid) for bid in bids]
If it's really, really important to do the replacement of each element "in place" (meaning don't create a new list), then the simplest-to-understand way is probably to loop over the indices:
for i in range(len(bids):
bids[i] = int(bids[i])
(But until you are very experienced and working with tons and tons of data, my strong recommendation is to not worry about "in-place" updating of lists. It's not as natural a way to write Python.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With