1

I am trying to fix this code but I keep getting the same Value Error:

value = (input('Please enter number: ')) total = 0 sum = 0 while value == value: count = float(input('Please enter another number: ')) sum =+ (1) total = count + count if count == (''): break average = (total+value/sum) print(float(average)) 
2
  • if you press enter without entering anything, it will return a ''. float cannot convert an empty string to an integer Commented Aug 13, 2021 at 4:29
  • The parameters of this code are that it repeatedly asks for the user to input a number and when they enter nothing it returns the average of all the numbers entered. How do it get around this? Commented Aug 13, 2021 at 4:32

4 Answers 4

1

You are immediately trying to cast your input to a float, which fails when something other than a number is entered. You could fix this by checking for a blank input before doing the cast:

... count = input('Please enter another number: ') if count == (''): break count = float(count) ... 

You do have a number of other logical errors in your code at this point, but that should at least get you past the value error.

Sign up to request clarification or add additional context in comments.

1 Comment

I was going to post the same thing as I mentioned in the comments (+1). But I think OP just needs to change the placement of float(). Also, mention that don't use sum as a variable because it is in-built function
0

You can try just using a try and catch statement to handle a possible ValueError:

try: count = float(input('Please enter another number: ')) except ValueError: break 

However, I do think it's worth noting that there are other parts of your code that are incorrect, so if you could give a bit more depth into what exactly you are trying to code, we can help you fix other parts if you need it.

Comments

0

Simply you can check weather the input is a number or not

value = (input('Please enter number: ')) total = 0 sum = 0 while value.isnumeric(): second_input = input('Please enter another number: ') if not second_input.isnumeric(): break count = float(second_input) sum =+ (1) total = count + count average = (total+value/sum) print(float(average)) 

Comments

0

Working code with exception handling:

value = "" while True: try: value = float(input('Please enter number: ')) break except: print("Invalid number") total = 0 sum = 1 # It is 1 if including the first value, can be change to 0 if isn't while value == value: try: count = input('Please enter another number: ') count = float(count) sum =+ (1) total = total + count # Changed this line as OP asking for average except ValueError: if count == (''): break else: print("Invalid number, press enter to exit loop.") average = (total+value/sum) print(float(average)) 

3 Comments

Please let me know if you have any clarifications, or there are parts of the code not performing correctly (logically).
Why 2 loops? It is pointless. The error is here: float(input('Please enter another number: ')).
I see what you mean, but there is an error in the previous steps that is causing another error, which is why there are two loops to verify the first value and second value(count). The error is at average = (total+value/sum) @Sujay

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.