编写代码,分别统计输入的正数和负数的个数,直到输入0结束。用到的知识点有:变量赋值, input 函数, while 语句,语句,格式化输出。

这个怎么编辑程序,用Python编辑运行不出来,和结果不对照


a=1
str1=[]
str2=[]
while a!=0:
    b=float(input())
    if b==0:
        a=0
    elif b>0:
        str1.append(b)
    else:
        str2.append(b)
print(f"正数个数为{len(str1)}")
print(f"负数个数为{len(str2)}")

这样?

img


while True:
    nums_list = input("请输入多个数字逗号间隔;输入‘0’结束:")
    try:
        dic = {"正数": 0, "负数": 0}
        for i in map(int, nums_list.replace(" ", "").split(",")):
            if i < 0:
                dic["负数"] += 1
            elif i > 0:
                dic["正数"] += 1
            else:
                print(dic)
                break
    except Exception as e:
        print(e)