'How to compare multiple values more efficiently?
Some simple code that compares prices from multiple stores, but I'm wondering what would happen if 10+ prices needed to be compared and the result to be only the most expensive/cheapest out of the 10+...
Is there an easier way to compare them without writing excess lines of code for efficiency? Maybe if statements?
walmart = input("Enter walmart price: ")
dollarstore = input("Enter Dollar store price: ")
amazon = input("Enter Amazons price: ")
exstore4 = input("Enter Example Store 4 price: ")
exstore5 = input("Enter Example Store 5 price: ")
if walmart <= dollarstore and dollarstore <= amazon:
print("Walmart has the cheapest price.")
elif dollarstore <= amazon and amazon <= walmart:
print("Dollar Store has the cheapest price.")
elif amazon <= walmart and walmart <= dollarstore:
print("Amazon has the cheapest price.")
elif amazon <= dollarstore and dollarstore <= walmart:
print("Amazon has the cheapest price.")
elif walmart == amazon and amazon == dollarstore:
print("All stores have the same price.")
else:
print("Something isnt right...")
Solution 1:[1]
Put them all in an array with name, then sort them and get their names
walmart = input("Enter walmart price: ")
dollarstore = input("Enter Dollar store price: ")
amazon = input("Enter Amazons price: ")
exstore4 = input("Enter Example Store 4 price: ")
exstore5 = input("Enter Example Store 5 price: ")
values = [
{"name": 'Wallmart', "value": walmart},
{"name": 'Dollar store', "value": dollarstore},
{"name": 'Amazons', "value": amazon},
{"name": 'Example Store 4', "value": exstore4},
{"name": 'Example Store 5', "value": exstore5},
]
values.sort(key=lambda a: a["value"])
# get only 1 store:
print(values[0]["name"] + ' has the cheapest price.')
# or find all cheapest store:
# Find last same price index
i=1
while i<len(values) and values[i]['value'] == values[0]['value']: i+=1
if i==len(values): print('All stores have the same price.')
elif i==1: print(values[0]["name"] + ' has the cheapest price.')
else: print(' and '.join([value["name"] for value in values[0:i]]) + ' have the cheapest price.')
Solution 2:[2]
walmart = input("Enter walmart price: ")
dollarstore = input("Enter Dollar store price: ")
amazon = input("Enter Amazons price: ")
exstore4 = input("Enter Example Store 4 price: ")
exstore5 = input("Enter Example Store 5 price: ")
i = (walmart, dollarstore, amazon, exstore4, exstore5)
cheapest = min(i)
print(cheapest)
if cheapest == walmart:
print("Walmart has the cheapest price of", cheapest)
elif cheapest == dollarstore:
print("Dollar Store has the cheapest price of", cheapest)
elif cheapest == amazon:
print("Amazon has the cheapest price of", cheapest)
elif walmart == amazon and amazon == dollarstore:
print("All stores have the same price.")
else:
print("Something isnt right...")
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 | namgold |
| Solution 2 | Prince Jam |
