1

Here's my code:

print('Welcome To The Balance Tracker!')
balance = float(input('Enter Your Starting Balance: ')

This is at the start of the program. How would I make it so the user cannot proceed unless the balance is a float, and if they enter anything else it shows an error message?

I know it probably has to be in a while loop. I'm just not a 100% sure on the execution.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Trader_M
  • 17
  • 1
  • 1
  • 2
  • Possible duplicate of [Python: How to keep repeating a program until a specific input is obtained?](http://stackoverflow.com/questions/20337489/python-how-to-keep-repeating-a-program-until-a-specific-input-is-obtained) – Matthias Apr 04 '17 at 06:52

3 Answers3

1

Catching the ValueError that is raised if float fails will solve your problem.

print('Welcome To The Balance Tracker!')
balance = None
while balance is None:
    try:
        balance = float(input('Enter Your Starting Balance: '))
    except ValueError:
        print("HEY! '{}' is not a float!".format(balance))

This will print a message if it's not float and while loop will not let user to go through!

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
abhinav
  • 1,108
  • 11
  • 23
  • This code will run in neither python2 (because there is no `input`) nor in python3 (because print requires parens). The inquirer was looking for python3. – Alex L Apr 04 '17 at 07:04
  • @AlesL your comment is self contradictory! Very first, this is for python-3, so there is no need to talk about python-2. First run this program, and then judge rather than just down voting any thing in your way! – abhinav Apr 04 '17 at 07:08
  • 2
    you have added parens around the final print, so now it will work! – Alex L Apr 04 '17 at 07:12
0

You can try

#!/usr/bin/env python3
is_balance_not_float = True
print('Welcome To The Balance Tracker!')
while (is_balance_not_float):
    try:
        balance = float(input('Enter Your Starting Balance: '))
    except ValueError:
        print("You are suppose to enter a number. ")
    else:
        is_balance_not_float = False

print("Balance : %s" % balance)
Abhilash Joseph
  • 1,196
  • 1
  • 14
  • 28
  • Why negative Vote ? – Abhilash Joseph Apr 04 '17 at 06:57
  • If you try running this, you will discover that there is a problem... `isinstance(balance, float)` is always going to return False, because input() always returns a string, never a numeric type. – Alex L Apr 04 '17 at 07:06
  • @AlexL everything is not meant to be down-voted. Comment, observe and then act. – abhinav Apr 04 '17 at 07:12
  • 1
    The problem with isinstance() here is that it tells you whether something IS a float, not whether it COULD BE CONVERTED to a float, which is really what you are trying to figure out...and the best way to determine that is probably to try to convert it, and see if it works... – Alex L Apr 04 '17 at 07:19
  • 1
    How could you except NameError? It's a ValueError and balance is outer from the scope at last line. – abhinav Apr 04 '17 at 07:19
  • @abhinav Typo :P – Abhilash Joseph Apr 04 '17 at 07:21
-2

The best thing to do is to try to convert the input to a float, and if it fails, then try again. Reasons for failure could include a string that is not convertible to float, or an EOF on stdin.

For instance:

balance = None
while balance is None:
    try:
        balance = float(input('Enter Your Starting Balance: '))
    except Exception:
        print('Balance must be a (floating point) number')

As a point of interest, if the user hits Control-C, it raises KeyboardInterrupt, which is not a subclass of Exception, and so will not get caught by the except, and will cause the program to exit.

Alex L
  • 1,114
  • 8
  • 11