转换空字符串inpu

2024-05-13 07:08:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个这样的代码来查找用户输入的数字的算术平均值,但由于某种原因,程序无法将字符串转换为浮点。我该换什么?

print("I'm going to find the arithmetical mean! \n")
jnr = 0; sum = 0
negative = "-"
while True:
    x = input(f"Type in {jnr}. nr. (To end press Enter): ")
    if negative not in x:
        jnr += 1
    elif negative in x:
        pass
    elif x == "": break
    sum = sum + float(x)

print("Aritmethmetical mean for these numbers is: "+str(round(sum/(jnr-1), 2)))

我发现了他的错误:

Traceback (most recent call last): File "C:\Users\siims\Desktop\Koolitööd\individuaalne proge.py", line 11, in sum = sum + float(x) ValueError: could not convert string to float


Tags: to代码用户innot数字算术float