所以我应该将巴比伦字符转换为数字值,以便:
"<" = 10
"T" = 1
"\" = 0
当我输入值时,它应该加上这些数字的总和,所以babylonian( "TTT")
等于3
,因为有3个“ 1”值。
到目前为止,我在下面有这段代码,但是我似乎无法获得总和,而只是获得了单个数字。
def babylonian(symbols) :
lst = []
for str in symbols:
sum = 0
for i in str:
if i == "<":
sum = sum + 10
elif i == "T":
sum = sum + 1
elif i == "\\":
sum = sum + 0
lst.append(sum)
return lst
答案 0 :(得分:1)
为什么不使用词典来保存映射?您的代码将变得更加简单
def babylonian(symbols) :
sum = 0
values = {'<' : 10, 'T': 1 , '\\': 1}
for str in symbols:
sum += values[str]
return sum
print(babylonian( "TTT"))