- # coding: UTF-8
- import os, sys
- import msvcrt
- import random
- import time
- dest = {1 : "Beijing", 2 : "Tianjin", 3 : "Shanghai", 4 : "Xi'an"}
- price = [10, 20, 30, 40]
- class Ticket():
- def __init__(self, destination, price):
- self.destination = destination
- self.price = price
- self.num = random.randint(0, 10)
- def __str__(self):
- return "Destination: " + self.destination + " \\r\\nPrice: " + str(self.price)
- def isAvailable(self):
- if self.num > 0:
- return True
- else:
- return False
- def buy(self, money):
- if money > t.price:
- print "TICKET:"
- print str(t)
- change = int(money) - t.price
- print "Change: " + str(change)
- print "Success!"
- raw_input("Press <Enter>")
- os.system("cls")
- self.save(money, change)
- elif money < t.price:
- print "The amount you paid is not enough. "
- print "Return: " + str(money)
- raw_input("Press <Enter>")
- os.system("cls")
- else:
- print "TICKET:"
- print str(t)
- print "Success!"
- raw_input("Press <Enter>")
- os.system("cls")
- self.save(money)
- def save(self, money, change = 0):
- file = open('ticket.log', 'a')
- s = self.__str__() + " Get: " + str(money) + " Change: " + str(change) + "\\r\\nTime: " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + "\\r\\n\\r\\n"
- file.write(s)
- file.close()
- if __name__ == '__main__':
- while True:
- print "----------Welcome to Ticket System!----------"
- print "----------Choose your destination: ----------"
- print "-----------------1. Beijing------------------"
- print "-----------------2. Tianjin------------------"
- print "-----------------3. Shanghai-----------------"
- print "-----------------4. Xi'an--------------------"
- print "-----------------0. Exit---------------------"
- ch = sys.stdin.readline()
- os.system("cls")
- if int(ch) > 0 and int(ch) < 5:
- destination = dest[int(ch)]
- p = price[int(ch) -1]
- t = Ticket(destination, p)
- #print "Ticket info:"
- #print str(t)
- if t.isAvailable():
- print str(t)
- ch = raw_input("Take one?(y/n)")
- if ch == 'y':
- money = raw_input("Enter the amount you pay: ")
- t.buy(int(money))
- else:
- os.system("cls")
- continue
- else:
- print "The tickets are not enough!"
- elif int(ch) == 0:
- sys.exit(0)
- else:
- print "Input the right choice, please."
- #该片段来自于http://www.codesnippet.cn/detail/250720134802.html
来源: http://www.codesnippet.cn/detail/250720134802.html