'How to delete "," at the end of the program python set
l = {"John","Evan"}
for i in l:
print(i,end=",")
How to get python to output: jhon,evan
not: jhon,evan,
?
Solution 1:[1]
If you had a list instead of a set, you could iterate by index and then prepend a comma starting with the second element.
l = {"John", "Evan"}
names = list(l)
for idx in range(len(names)):
if idx > 0:
print(",", end="")
print(names[idx], end="")
# John,Evan
Solution 2:[2]
This is really an opinion-based question (alternatively, it's one that is not answerable given the provided information), because it depends on what is going to be valued by the business and for user-experience.
You can go for absolute consistency and implement some sort of lease system in the inventory where adding items in the cart reserves them and if someone leaves the cart unattended for too long, a system clears the cart and releases the lease. The upside is that you can make a stronger promise to someone that putting a phone in the cart means they'll get a phone (probably good for UX and customer trust), the downside is when someone is dithering with the cart and the phone is showing as unavailable to others (probably not good for UX and customer trust). You can mitigate that by having a shorter timeout before the cart is canceled, but how do you think a customer will feel if they added a phone to their cart and had to answer an urgent message then came back and found the cart emptied?
The lease approach also can't make the promise absolute: just because the inventory service says there are 10 phones in stock doesn't mean there actually are 10 saleable phones in stock. How do you account for the case where someone puts a phone in the cart and a forklift driver in your warehouse drives into the section of shelving where those 10 phones are stored, destroying them? You're not escaping the obligation to have a process for dealing with accepting an order for something that turns out to not be in stock.
Admittedly, you can increase the absoluteness of the promise by not actually adding the item to the cart until a human picks the item and puts in a cart: the latency and extra cost there might not be desirable though.
Alternatively, one can build around the recovery process. Allow conflicting orders, but don't attempt to charge until the item is picked. If there are more orders than items, then you offer the folks who don't get the items compensation of some sort ("We're sorry we can't deliver this right now. Here's a half-off voucher for your next purchase"), or you might effectively go to an auction to see which customer will accept the least compensation in exchange for getting the product later. You can asynchronously communicate the out-of-stock status to the services displaying the product catalog and managing the cart, to reduce the window in which there's inconsistency.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Biegeleisen |
Solution 2 | Levi Ramsey |