-3

Im new to python and im currently doing a practise sac for IT and im trying to get the program to read the first digit of the postcode and then set the number to the billing variable. When i run it, it just sets it as the first if statement even though the first number is not a 0 or a 6. Please help me out!!!![enter image description here][1]

#commision = 1.125 For later on
#bid = 100 part of prac sac
total_cost = str(0)
billing = 0

for i in range(100000):
    postcode = input("What is your postcode?: \n")
    if len(postcode) == 4:
        break
    else:
        print("Postcode invalid. Please retype postcode.")
        continue

print(postcode)
print(postcode[0])

# This is where im having trouble
if (postcode[0]) == str(0) or str(0):
    billing = 25

elif (postcode[0]) == str(1) or str(2) or str(5):
    billing = 15

elif (postcode[0]) == str(3):
    billing = 12

else:
    billing = 20

print(billing)
  • you probably want to use `while True:` instead of the `for-loop`. The `continue` is unnecessary. – Daniel Apr 28 '15 at 09:55
  • simply use a dictionary: `billing = {'0':25, '1':15, '2':15, '3':12, '5':15}.get(postcode[0], 20)`. – Daniel Apr 28 '15 at 09:57

1 Answers1

0

Your if statements are not working correctly because you are using the logical operators and, or in wrong manner, As these operators only compare two boolean values so you need to write elif (postcode[0]) == str(1) or (postcode[0]) == str(2) or (postcode[0]) == str(5): for each of the if statements, to compare two strings and return a boolean to apply these logical operators on.

if (postcode[0]) == "0" or (postcode[0]) == "6":
    billing = 25

elif (postcode[0]) == "1" or (postcode[0]) == "2" or (postcode[0]) == "5":
    billing = 15

elif (postcode[0]) == "3":
    billing = 12

else:
    billing = 20
ZdaR
  • 22,343
  • 7
  • 66
  • 87