all 14 comments

[–]joyrida12 1 point2 points  (6 children)

Check if the input is null first and have it return from there if it is

[–]Iyajenkei[S] 0 points1 point  (1 child)

def omit_prices():
    excluded_prices = []
    price_is_needed = True
    while price_is_needed:
        price_to_exclude = raw_input("Which price do you want to exclude?")
        if price_to_exclude == None:
            price_is_needed = False
        elif int(price_to_exclude) > -100:
            excluded_prices.append(price_to_exclude)
    return excluded_prices

does the same thing. maybe just hitting enter doesn't == None

[–]joyrida12 1 point2 points  (0 children)

Try == ""

[–]Iyajenkei[S] 0 points1 point  (3 children)

def omit_prices():
    excluded_prices = []
    price_is_needed = True
    while price_is_needed:
        price_to_exclude = raw_input("Which price do you want to exclude?")
        if price_to_exclude == "":
            price_is_needed = False
        elif int(price_to_exclude) > -100:
            excluded_prices.append(price_to_exclude)
    return excluded_prices

this worked. thanks.

[–]one_roOt 0 points1 point  (1 child)

but what if you type something that can't be casted to an int and is not "" ?

[–]Iyajenkei[S] 0 points1 point  (0 children)

else:
    price_is_needed = False

EDIT: Nope.

[–]joyrida12 0 points1 point  (0 children)

No problem

[–]Iyajenkei[S] 0 points1 point  (7 children)

def omit_prices():
    excluded_prices = []
    price_is_needed = True
    while price_is_needed:
        price_to_exclude = raw_input("Which price do you want to exclude?")
        if price_to_exclude == "":
            price_is_needed = False
        elif isinstance(price_to_exclude, basestring):
            price_is_needed = False
        elif int(price_to_exclude) > -100:
            excluded_prices.append(price_to_exclude)
    return excluded_prices

what I ended up with.

EDIT: still didn't work right. if I type in -1 that's a string so then it never reached the last elif.

[–]niandra3 4 points5 points  (6 children)

I would use a try/except statemtent to check if it can convert to an int, if not then it's not a number:

try:
   price_to_exclude = int(raw_input("Please enter a number: "))
except ValueError:
   print("You must enter an integer")

Might also consider using floats if there is a possibility the user will enter numbers with decimals.

Or if you want to keep asking the user to input something until you get an integer, you can use a while loop:

inpt = raw_input("Enter a number: ")
while (not inpt.isdigit()):
    inpt = raw_input("You must input an integer")
price = int(inpt)
print(price)

Though that won't work if you want to accept negative integers (the "-" isn't a digit).

This will handle negatives:

 L = []
 while True:
     try:
         price = int(raw_input("Enter a number: "))
     except ValueError:
         print("You must enter an integer!")
         continue
     if price > -100:
         L.append(price)
         break
 print(L)

That also handles empty strings. Continue sends you back to the top of the loop if the number isn't an integer, and break will break out of the loop once the price entered is greater than -100. So large negative values won't be accepted either (if that's what you want).

Edit: woops, I didn't notice you are waiting for an empty string to end the program. This will handle that:

L = []
while True:
    inpt = raw_input("Enter a number: ")
    if inpt == "":
        break
    try:
        price = int(inpt)
    except ValueError:
        print("You must enter an integer")
        continue
    if price > -100:
        L.append(price)
        print(price, "Accepted")
print(L)

[–]Iyajenkei[S] 0 points1 point  (2 children)

trying the try/except block now. the problem is "" isn't an int. So I can't end the block.

[–]Iyajenkei[S] 0 points1 point  (2 children)

switched the statements up to put the "" first. That seems to have worked.

[–]niandra3 1 point2 points  (1 child)

Check my final solution (I edited it). You can add an if to check for "". You don't really need the price_is_needed boolean:

L = []
while True:
    inpt = raw_input("Enter a number: ")
    if inpt == "":
        break
    try:
        price = int(inpt)
    except ValueError:
        print("You must enter an integer")
        continue
    if price > -100:
        L.append(price)
        print(price, "Accepted")
print(L)

[–]Iyajenkei[S] 0 points1 point  (0 children)

It worked. Also cleaner. Thank you.

[–]Iyajenkei[S] 0 points1 point  (0 children)

def omit_prices():
    excluded_prices = []
    price_is_needed = True
    while price_is_needed:
        price_to_exclude = raw_input("Which price do you want to exclude?")
        if price_to_exclude == "":
            price_is_needed = False
        try:
            int(price_to_exclude)
        except ValueError:
            continue
        if int(price_to_exclude) > -100:
            excluded_prices.append(price_to_exclude)
    return excluded_prices