0

in my program the user can input only one of these codes: 86947367 , 78364721 , 35619833 , 84716491 , 46389121 How can I check which one the user has entered with using if statement , an easier method ? part of my code :

if (items[0]) == 86947367 :
with open("read_it.txt") as text_file:
    try:
        price = int(text_file.readlines()[2])
    except ValueError:
        print("error")
    else:
        new_price = int(price * (items2[0]))
        print("£",new_price)

elif (items[0]) == 78364721 :
with open("read_it.txt") as text_file:
    try:
        price1 = int(text_file.readlines()[6])
    except ValueError:
        print("error")
    else:
        new_price1 = int(price1 * (items2[0]))
        print("£",new_price1)

elif (items[0]) == 35619833 :
with open("read_it.txt") as text_file:
    try:
        price2 = int(text_file.readlines()[10])
    except ValueError:
        print("error")
    else:
        new_price2 = int(price2 * (items2[0]))
        print("£",new_price2)


elif (items[0]) == 84716491  :
with open("read_it.txt") as text_file:
    try:
        price3 = int(text_file.readlines()[14])
    except ValueError:
        print("error")
    else:
        new_price3 = int(price3 * (items2[0]))
        print("£",new_price3)

NO VERY NEAT IS IT ? THATS WHY I AM ASKING IF AND HOW CAN MY PROGRAM LOOK AT WHAT THE USER HAS ENETERED AND ACCORIDNG TO THAT DO THE FOLLOWING PORCEDURE WITHOUT GOING THROUGH EACH CODE LIKE I HAVE DONE (THIS IS ONLY ONE POART OF MY CODE ).

  • You write code? This isn't a code-writing service. YOU write code, we (maybe) try help fix it. – Marc B Aug 25 '16 at 15:57
  • Use the [membership operator](https://docs.python.org/3/reference/expressions.html#in) – Phylogenesis Aug 25 '16 at 16:02
  • LOL , I love people who say that , how can you right my code if you do not even know what my program does ? –  Aug 25 '16 at 16:42

0 Answers0