Python问题的解决(有没有会的)

问题遇到的现象和发生背景

问题相关代码,请勿粘贴截图

运行结果及报错内容

img

img

读取每行用.split()以空格分割,并用float()转为数值计算即可
你题目的解答代码如下:

with open(r'suanshi.txt', 'r', encoding='utf-8') as rf,open(r'jieguo.txt', 'w', encoding='utf-8') as wf:
    for d in rf.readlines():
        li = d.strip().split()
        a = float(li[0])
        b = float(li[1])
        print(f"{a:.1f}+{b:.1f}={a+b:.1f}")
        wf.write(f"{a:.1f}+{b:.1f}={a+b:.1f}\n")

img

img

第二个

s = input()
dic1 = {}
dic2 = {}
for v in s:
    if v.islower():
        dic1[v] = dic1.get(v,0)+1
    if v.isupper():
        dic2[v] = dic2.get(v,0)+1
li1 = sorted(dic1.items(),key=lambda x: x[0])
li2 = sorted(dic2.items(),key=lambda x: x[0])
for c,v in li1:
    print(f'{c} {v}')
for c,v in li2:
    print(f'{c} {v}')

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

a=open("suanshi.txt","r")
d=open("jieguo.txt","w")
for i in a:
    b=[]
    b=i.split()
    b[0]=eval(b[0])/1.0
    b[1]=eval(b[1])/1.0
    c=b[0]+b[1]
    e=str(b[0])+'+'+str(b[1])+'='+str(c)
    d.write(e)
    d.write('\n')
a.close()
d.close()

img

img

1、

a=open("suanshi.txt","r")
d=open("jieguo.txt","w")
for i in a:
    b=[]
    b=i.split()
    b[0]=eval(b[0])/1.0
    b[1]=eval(b[1])/1.0
    c=b[0]+b[1]
    e=str(b[0])+'+'+str(b[1])+'='+str(c)
    d.write(e)
    d.write('\n')
a.close()
d.close()

2、

s = input()
dic1 = {}
dic2 = {}
for v in s:
    if v.islower():
        dic1[v] = dic1.get(v,0)+1
    if v.isupper():
        dic2[v] = dic2.get(v,0)+1
li1 = sorted(dic1.items(),key=lambda x: x[0])
li2 = sorted(dic2.items(),key=lambda x: x[0])
for c,v in li1:
    print(f'{c} {v}')
for c,v in li2:
    print(f'{c} {v}')

3、

str =  '78940ADDddQW'
dict = {}
for w1 in set(str):
      if "a" <= w1 <= "z" or "A" <= w1 <= "Z":
           dict[w1] = str.count(w1)
sorted(dict.items(), key=lambda x:x[0])
print(dict)