0

I'm trying to write a 'while' loop that takes a users input, if it is a number it remembers it, if it is a blank space it breaks. At the end it should print the average of all entered numbers. This is giving me the error 'could not convert string to float: '. What exactly is wrong here? Thanks!

EDIT: I re-wrote it like this and I get the same error about converting, but it seems to be on the final (count += 1) line?

number = 0.0
count = 0

while True:
    user_number = input('Enter a number: ')
    if user_number == ' ':
        break
        print (number / count)
    number = number + float(user_number)
    count += 1
3
  • The issue is in the order, even if you enter blank space, you still try to convert it to float in - number = number + float(user_number) . Commented Aug 18, 2017 at 5:03
  • Do your check first, then add. Commented Aug 18, 2017 at 5:05
  • Is the user entering characters other than ' ' and numbers? Commented Aug 18, 2017 at 5:11

5 Answers 5

2

My guess is that you directly hit enter when you don't want to pass numbers anymore. In that case, comparing with a space is incorrect.

number = 0.0
count = 0

while True:
    user_number = input('Enter a number: ')
    if user_number == '':
        break

    number += float(user_number)
    count += 1

print (number / count)

Also a statement after a break is unreachable.


If you want a cleaner alternative, I would recommend appending to a list, and then computing the average. This removes the need for a separate counter. Try this:

numbers = []
while True:
    user_number = input('Enter a number: ')
    if user_number == '':
        break            

    numbers.append(float(user_number))

print (sum(numbers) / len(numbers))

Additionally, you could remove the need for a break by testing in the head of while, but you'll need to take an additional input outside the loop.

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

1 Comment

Ah, that's it! It wasn't a blank space, just nothing! Thank you so much.
0

You should change the order, right now you try to convert everything into floats, even blank spaces.

while True:
    user_number = input('Enter a number: ')
    if not user_number.isdigit():
        print (number / count)
        break
    count += 1
    number = number + float(user_number)

Additionally, you should do the print of the average value before the break.

I changed your if condition to break if any input except a number is entered, it should be a bit more general than before.

2 Comments

@G3ck0 I changed it to break when anything except a number is entered. It accounts for cases where other string than numbers and blank spaces are used.
It's a program for uni, weekly exercises. Nothing will one entered other than numbers or a blank space.
0

Change order and it should solve the problem, you should first check is enterned input is string or not then go to number part

#!/usr/bin/python
number = 0.0
count = 0

while True:
    user_number = raw_input('Enter a number: ')
    if user_number == (' '):        
        print (number / count)
    break
    number = number + float(user_number)
    count += 1

1 Comment

Even that still gives me the 'could not convert string to float:' error...
0

This should work for you.

lst= []
while True:
user_number = input('Enter a number: ')
if user_number == ' ':
    break
lst.append(int(user_number))
print(int(user_number))
print("Average : " + str(sum(lst)/len(lst)))  

3 Comments

and what you think my posting similar answer?
It certainly does not work, because it suffers from severe formatting and indentation problems.
@cᴏʟᴅsᴘᴇᴇᴅ.. you are right , my code has indentation issues as it appears here..because i am new to this community and not exactly sure how to put my working code in the comments. I apologize, but my code certainly works perfect, i have tested it before posting the code..please apply appropriate indentation to my code..it will work
0

This code works correctly. I think you are giving Other than space as input to break.

number = 0.0
count = 0

while True:
    user_number = input('Enter a number: ')
    if user_number == ' ':
        break
        print (number / count)
    number = number + float(user_number)
    count += 1

or else use below code: --> Break will be triggered for non-digit input.

number = 0.0
count = 0
while True:
    user_number = input('Enter a number: ')
    if not user_number.isdigit():
        break
    number = number + float(user_number)
    count += 1
print("user_number", number)
print("Count", count)
print (number / count)

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.