70分Python 求助

P1957 口算练习题

btxrrvt @ 2022-09-01 12:00:46

n = int(input())
res = []
for i in range(n):
    ans = input().split()
    if len(ans) == 2:
        ans.insert(0, 'a')
    res.append(ans)
for i in res:
    a1 = i[1]
    fh = "+" if i[0] == "a" else ("-" if i[0] == "b" else "*")
    a2 = i[2]
    wz = a1 + fh + a2
    print("{}{}{}={}".format(a1, fh, a2, eval(wz)))
    print(len(wz)  + 1 + len(str(eval(wz))))

by Terrible @ 2022-09-01 12:58:33

@Gionta 若该行为两个数据,则表示本题的运算类型与上一题的运算类型相同,而这两个数据为运算数。


by btxrrvt @ 2022-09-01 17:38:44

@Terrible 谢谢你,是我粗心了

for i in range(n):
    ans = input().split()
    if len(ans) == 2:
        if i != 0:
            ans.insert(0, res[i - 1][0])
    res.append(ans)

|