Program do liczenia cen

0

Cześć,

Napisałem sobie prosty programik który ma obliczać ceny biletów w zależności od przejechanych stacji.
Każda przejchana stacja to np. 10zł.
Poniżej kod, jest możliwość napisania tego krócej? Sam ręcznie musiałem wypisać wszystkie możliwie warianty rozpoczęcia oraz zakończenia podróży.

station = ("Londyn ->", "<- Station1 ->", "<- Station2 ->", "<- Manchester")

print("Select start and end of your journej")
print("Train road below")
print(station)

print("Select start")
start = input()
print("Select End")
end = input()

print("You select road from " + start + " to " + end)

if start == "Londyn" or start == "londyn":
if end == "Manchester" or end == "manchester":
print("The cost of travel 30 pound")
elif end == "Station2" or end == "station2":
print("The cost of travel 20 pound")
elif end == "Station1" or end == "station1":
print("The cost of travel 10 pound")

if start == "station1" or start == "Station1":
if end == "Manchester" or end == "manchester":
print("The cost of travel 20 pound")
elif end == "Station2" or end == "station2":
print("The cost of travel 10 pound")
elif end == "Londyn" or end == "londyn":
print("The cost of travel 10 pound")

if start == "Station2" or start == "station2":
if end == "Manchester" or end == "manchester":
print("The cost of travel 10 pound")
elif end == "Station1" or end == "station1":
print("The cost of travel 10 pound")
elif end == "Londyn" or end == "londyn":
print("The cost of travel 20 pound")

if start == "Manchester" or start == "manchester":
if end == "Londyn" or end == "londyn":
print("The cost of travel 30 pound")
elif end == "Station1" or end == "station1":
print("The cost of travel 20 pound")
elif end == "Londyn" or end == "londyn":
print("The cost of travel 30 pound")

1

Nie prościej użyć słownika?

import math

stations = {"London":1, "Station1":2, "Station2":3, "Manchester":4}

print("Select start and end of your journey")
print("Train road below")
print(*stations.keys())

print("Select start")
start = input()
print("Select End")
end = input()

print("You select road from " + start + " to " + end)
cost = math.abs(stations[end] - stations[start]) * 10
print("The cost of travel is {} pound".format(cost))
0

Oczywiście że prościej, myślałem aby zastosować słownik...
Wklepałem Twój kod, jednak wywala mi błąd: "AttributeError: module 'math' has no attribute 'abs'"

1 użytkowników online, w tym zalogowanych: 0, gości: 1