3

I made a code like this to find the arithmetical mean for numbers the user has typed in but for some reason the program couldn't convert string to float in the end. What should I change?

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)))

I got his Error :

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

9
  • using round() is the reason for your problem. Commented Dec 12, 2017 at 19:38
  • What should I do about the round(). Just take it out? Commented Dec 12, 2017 at 19:42
  • 2
    What is the error that you get? Please provide any tracebacks or input or output in your question. Commented Dec 12, 2017 at 19:42
  • round() will not display a float value. Commented Dec 12, 2017 at 19:43
  • 3
    round() is not the error. The error comes from the fact that you're trying to call float(x) which is an invalid string to convert when the user press enter. Commented Dec 12, 2017 at 19:43

3 Answers 3

3

As I said in my comment the error comes from the fact that calling float(x) when the user uses Enter result in the error. The easiest way to fix your code without changing everything is by checking first if the input is "". That way you will not be trying to convert an empty string to float.

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 x == "":
        break
    elif negative not in x:
        jnr += 1
        sum = sum + float(x)


print("Aritmethmetical mean for these numbers is: "+str(round(sum/(jnr-1), 2)))
Sign up to request clarification or add additional context in comments.

5 Comments

@FredLarson can you please elaborate? I can't understand what you're underlying.
If negative not in x is false, negative in x must be true. The second test is redundant, so eliminate it. Just else: will do.
Brain fade -- I had said elif: when I meant else:. Sorry.
@FredLarson Ok haha, now I see. I wanted to keep the code as original as possible. You are right, but using else would also be redundant in my opinion, since else: pass will be ignored by the compiler...
You're right, else: pass doesn't make a whole lot of sense. Good work.
0

You're trying to convert a string to a float but it is an invalid string if you are using the string for that purpose. I would just continue asking until the user gives the right input, like this:

def float_input(s):
    while True:
        try:
            x = input(s)
        except ValueError:
            print('Invalid input. Try again.')
        else:
            break
    return x

Then, instead of input, use float_input in your code.

Comments

0

An updated version of your code :

print("I'm going to find the arithmetical mean! \n")
jnr = 0; sum = 0
while True:
    x = int(input())
    if x>1:
        jnr += 1
    elif x<1:
        pass

    if x == 0:
        break
    sum += float(x)
    print(sum)



print("Aritmethmetical mean for these numbers is: {}".format(sum/jnr))

output:

I'm going to find the arithmetical mean! 

9
9
9
9
9
0
Aritmethmetical mean for these numbers is: 9.0

Pythonic way:

You can find mean by:

print("I'm going to find the arithmetical mean! \n")
inp=[int(i) for i in input().split()]
print(sum(inp)/len(inp))

output:

I'm going to find the arithmetical mean! 

9 9 9 9 9
9.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.