1

I'm trying to create a calculator with the following code:

number_1 = " "
while number_1 not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
    number_1 = raw_input("Please enter your first number:")
    if number_1 not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
        print "ERROR: Please type a valid number:"


print number_1
operator_symbol = " "
while operator_symbol not in ["+", "-", "*", "/"]:
    operator_symbol = raw_input("Please enter an appropriate operator symbol:")
    if operator_symbol not in ["+", "-", "*", "/"]:
        print "ERROR: Please type one of the following operator symbols: +, -, *, /."
print operator_symbol


number_2 = " "
while number_2 not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
    number_2 = raw_input("Please enter your second number:")
    if number_2 not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
            print "ERROR: Please type a valid number"
print number_2

They are spaced out while I work on them. For number_1 and number_2 I have defined the characters that I want to be used ('0'-'9'), yet when I use a number larger than 9, it gives an error.

I want to use '0'-'9' as characters available, not the specific numbers.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
poddpython
  • 45
  • 1
  • 8

2 Answers2

0

You are checking if number_1 is a single digit (one of the elements in your list).

What you want to do instead is check that number_1 is a string only containing digits.
You can do this using the .isdigit() method.

Also, instead of double checks (both in the while and in the if), you should only do the check once:

number_1 = raw_input("Please enter your first number:")
while not number_1.isdigit()
    number_1 = raw_input("ERROR: Please type a valid number:")
stranac
  • 26,638
  • 5
  • 25
  • 30
  • Thanks, what else do I need to change and how do I account for the situation where someone will type a character other than a digit? Thanks again. – poddpython Feb 24 '15 at 11:12
  • I edited my answer, maybe this makes it easier to understand. – stranac Feb 24 '15 at 12:04
0

Using the .isdigit() method of strings you can check whether the input supplied is a number or not.

On how to use it, you can do the following:

number_1= raw_input("Enter your first number")
while not number_1.isdigit():
    number_1 = raw_input("Error: Please enter a valid number: ")

Similarly for number_2.. The trick is asking for the number as a input first and if the number is not a number then continue asking the user for the number

That should do.

geekpradd
  • 418
  • 2
  • 9
  • Hi mate, this is helpful but it doesn't seem to work. If I type a character other than a digit, it re-prints the original question instead of a reply to the error. :S – poddpython Feb 24 '15 at 11:31
  • It's working in my machine.. Do you need to stop asking for input? Then the code will be different.. – geekpradd Feb 24 '15 at 11:45
  • This is a image to see it in action: [http://postimg.org/image/eegtihz8p/](http://postimg.org/image/eegtihz8p/) – geekpradd Feb 24 '15 at 11:49
  • I have everything sorted, BUT, for either number_1 or number_2, if the user types anything other than a digit, it re-prints the "Please enter your first number" instead of printing "ERROR: Please enter a valid number". Sorry i'm just confused as to why it's ignoring it. – poddpython Feb 24 '15 at 11:51
  • I understood what you want.. I'm editing my answer for you. Kindly wait – geekpradd Feb 24 '15 at 11:52
  • Modified.. I hope it helps. – geekpradd Feb 24 '15 at 11:55
  • Everything is working... finally lol. Thanks for your help!:) – poddpython Feb 24 '15 at 12:03