[閒聊] Mushclient中文數字轉阿拉伯數字Python版

作者: rsliang (rsliang)   2015-09-18 02:22:27
這個是剛好看到Mushclient有支援Python的script,閒閒無事拿來玩玩看的成果,
理論上是沒有bug啦,有的話請和我說,我再修修看,目前還是弱弱的Python新手。
說明:
一、__init__底下宣告的對應數字使用dict有更漂亮的寫法,剛開始寫的時候沒想
到,寫完了才想起來,所以就懶得改了。
二、純練Python用,很多地方的邏輯繞很多圈或是有多餘的宣告,請鞭小力一點:p
=================================Code====================================
import os,sys
class chs2num:
def __init__(self):
self.money = None
self.total = 0
self.num = 0
self.hunmillion = 100000000
self.tenthousand = 10000
self.thousand = 1000
self.hundred = 100
self.ten = 10
self.one = 1
self.two = 2
self.three = 3
self.four = 4
self.five = 5
self.six = 6
self.seven = 7
self.eight = 8
self.nine = 9
self.zero = 0
self.curnum = 0
def tonum(self, money):
self.money = money.decode('utf-8')
for self.num in range(0, len(self.money)):
world.Note(self.money[self.num])
if self.money[self.num] == u"一":
self.curnum = self.one
world.Note(self.curnum)
elif self.money[self.num] == u"二" or
(self.money[self.num] == u"兩" and self.num != (len(self.money) - 1)):
self.curnum = self.two
elif self.money[self.num] == u"三":
self.curnum = self.three
elif self.money[self.num] == u"四":
self.curnum = self.four
elif self.money[self.num] == u"五":
self.curnum = self.five
elif self.money[self.num] == u"六":
self.curnum = self.six
elif self.money[self.num] == u"七":
self.curnum = self.seven
elif self.money[self.num] == u"八":
self.curnum = self.eight
elif self.money[self.num] == u"九":
self.curnum = self.nine
elif self.money[self.num] == u"零":
self.curnum = self.zero
elif self.money[self.num] == u"十":
if self.curnum == 0:
self.total = self.total + self.ten
else:
self.total = self.total + self.curnum * self.ten
self.curnum = 0
elif self.money[self.num] == u"百":
self.total = self.total + self.curnum * self.hundred
self.curnum = 0
elif self.money[self.num] == u"千":
self.total = self.total + self.curnum * self.thousand
self.curnum = 0
elif self.money[self.num] == u"萬":
if self.curnum == 0:
self.total = self.total * self.tenthousand
else:
self.total = (self.total + self.curnum) * self.tenthousand
self.curnum = 0
elif self.money[self.num] == u"億":
if self.curnum == 0:
self.total = self.total * self.hunmillion
else:
self.total = (self.total + self.curnum) * self.hunmillion
self.curnum = 0
if self.num == (len(self.money) - 1):
if self.curnum < 10 and self.curnum > 0:
self.total = self.total + self.curnum
return self.total
作者: laechan (揮淚斬馬雲)   2015-09-19 16:20:00
你還可以增加一個"兩"的額外轉換

Links booklink

Contact Us: admin [ a t ] ucptt.com