Skip to main content
added 62 characters in body
Source Link
Cabrera
  • 181
  • 2
  • 4

Heres some quick suggestions...


Youredit: as pointed out, isdigit() only works on integers
Your is_number(item) function could be the built-in isdigit():

if a_list[count].isdigit() and ...

You could chain together method calls, doesn't make it less readable imo:

astring = raw_input("Calculation: ").replace(" ", "")

The first two loops you do could be condensed into one:

# Next it will add only supported characters to the list
a_list = []
for item in astring:
    if item not in set(["0", "1", "2", "3" , "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", ".", "(", ")"]):
        print ("Unsupported Character: " + item)
        exit()
    a_list.append(item)

Also, try to use better variable names than astring and a_list.

Heres some quick suggestions...


Your is_number(item) function could be the built-in isdigit():

if a_list[count].isdigit() and ...

You could chain together method calls, doesn't make it less readable imo:

astring = raw_input("Calculation: ").replace(" ", "")

The first two loops you do could be condensed into one:

# Next it will add only supported characters to the list
a_list = []
for item in astring:
    if item not in set(["0", "1", "2", "3" , "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", ".", "(", ")"]):
        print ("Unsupported Character: " + item)
        exit()
    a_list.append(item)

Also, try to use better variable names than astring and a_list.

Heres some quick suggestions...


edit: as pointed out, isdigit() only works on integers
Your is_number(item) function could be the built-in isdigit():

if a_list[count].isdigit() and ...

You could chain together method calls, doesn't make it less readable imo:

astring = raw_input("Calculation: ").replace(" ", "")

The first two loops you do could be condensed into one:

# Next it will add only supported characters to the list
a_list = []
for item in astring:
    if item not in set(["0", "1", "2", "3" , "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", ".", "(", ")"]):
        print ("Unsupported Character: " + item)
        exit()
    a_list.append(item)

Also, try to use better variable names than astring and a_list.

Source Link
Cabrera
  • 181
  • 2
  • 4

Heres some quick suggestions...


Your is_number(item) function could be the built-in isdigit():

if a_list[count].isdigit() and ...

You could chain together method calls, doesn't make it less readable imo:

astring = raw_input("Calculation: ").replace(" ", "")

The first two loops you do could be condensed into one:

# Next it will add only supported characters to the list
a_list = []
for item in astring:
    if item not in set(["0", "1", "2", "3" , "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", ".", "(", ")"]):
        print ("Unsupported Character: " + item)
        exit()
    a_list.append(item)

Also, try to use better variable names than astring and a_list.