Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/usr/bin/python
- # -*- coding: latin-1 -*-
- import sys
- print "Welcome to Merchant\'s Guide To The Galaxy"
- print "Type a command and then press CTRL + D :"
- symbols = {
- "i" : 1,
- "v" : 5,
- "x" : 10,
- "l" : 50,
- "c" : 100,
- "d" : 500,
- "m" : 100,
- }
- alg = {}
- ideal = {}
- def note(t):
- """give back output"""
- print t
- def check(term):
- """analyse a kind of number"""
- x = 0
- try:
- y = [alg[i] for i in term]
- except:
- return -1
- while y:
- z = y.pop(0)
- if y and y[0] > z:
- x -= z
- else:
- x += z
- return x
- def function(sentence):
- """analyse the sentence"""
- term = sentence.lower().split(None)
- if not term:
- return
- if len(term) == 3 and term[1] == "is":
- cod = term[0]
- obj = term[2]
- if not obj in symbols:
- return note("'%t' is not a valid format." % obj)
- alg[cod] = symbols[obj]
- return
- if len(term) > 4 and term[-1] == "credits" and term[-3] == "is":
- term.pop()
- try:
- obj = float(term[-1])
- except:
- return note("'%t' is not a valid numeric value" % term[-1])
- term.pop()
- term.pop()
- p = term.pop()
- x = check(term)
- if x < 0:
- return note("Not a valid type")
- ideal[p] = obj / x
- return
- if term[0:3] == ["how", "much", "is"]:
- term = term[3:]
- if term[-1] == "?":
- term.pop()
- x = check(term)
- if x < 0:
- return note("'%t' isn't a Galactic term" % " ".join(term))
- print " ".join(term), "is", x
- return
- if term[0:4] == ["how", "many", "credits", "is"]:
- term = term[4:]
- if term[-1] == "?":
- term.pop()
- p = term.pop()
- if not p in ideal:
- return note("There's no information about %t" % p)
- x = check(term)
- if x < 0:
- return note("'%t' isn't a Galactic term" % " ".join(term))
- print " ".join(term), p.title(), "is", int(x * ideal[p]), "Credits"
- return
- return note("I've no idea what you are talking about")
- for sentence in sys.stdin:
- function(sentence)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement