1

trying to make a program that says, when it's an estonian post code or not. Estonian post code has 5 numbers. first number is between 1 and 9. Other four numbers are between 0 and 9. My code at the moment:

print("insert postcode")
    inserted_code = input()

if (inserted_code[0] > "0" and inserted_code[1-4] >= "0"):

    print("Estonian postcode")

elif (inserted_code[0] == "0"):

    print("Not an estonian code")

else:
    print("Not an estonian code")

My question is, how can I mak it that way, that the program knows that Estonian post code has 5 numbers?

Martin Rehkli
  • 31
  • 1
  • 6
  • 2
    Why not just test that `code >= 10000 and code <= 99999`? Or if it absolutely needs to be a string, use a regex, e.g. `/[1-9]\d{4}/`. – Biffen Mar 23 '16 at 12:57
  • If I'm not wrong, then it doesen't say that Estonian code has 5 numbers? Or does it? Sorry, I've been on Python for 2 weeks or so only. – Martin Rehkli Mar 23 '16 at 12:59
  • What do you think `inserted_code[1-4]` does? It does *not* look at characters 2-5... – Tim Pietzcker Mar 23 '16 at 12:59
  • @MartinRehkli Which number hasn't got 5 digits and is smaller than 10,000 or greater than 99,999? – Biffen Mar 23 '16 at 13:00
  • inserted_code[1-4] > "0" Thought it tells that numbers 1-4 must be bigger or equal 0. Ye sorry, my mistake, i forgot to add equals symbol. :D – Martin Rehkli Mar 23 '16 at 13:01
  • @MartinRehkli `> "0"` does *not* mean ‘is a digit’. Note that `":" > "0"` → `True`, `"?" > "0"` → `True`, etc. – Biffen Mar 23 '16 at 13:02
  • Thank you guys for help. I'm going to try how to get it like I want it. Cheers! – Martin Rehkli Mar 23 '16 at 13:08
  • You need to first check to see whether or not the code is an integer - http://stackoverflow.com/questions/36136484/python-3-1-problems-with-else-in-my-code/36136619#36136619 then you can check the range (see my answer to the question) – PyNEwbie Mar 23 '16 at 13:14

2 Answers2

0

I suggest you doing this:

  1. use input("text") instead print description before each input()
  2. inserted_code[1:] stand for every char except very first one
  3. please, check that length of code equals 5 chars.
  inserted_code = input("insert postcode")
  if int(inserted_code[0]) > 0 and all([int(last_num) >= 0 for last_num in inserted_code[1:]]) and len(inserted_code) == 5:
      print("Estonian postcode")
  else:
      print("Probably, Belarusian code")
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • hmm, shouldn't first line be like this: inserted_code = int(input("insert postcode: ")) ?? But it's weird, cause' in both ways it gives error for the first line – Martin Rehkli Mar 23 '16 at 13:28
  • Sure, it's up to you to handle exception cases correctly. But my code should work for fair `int` input. Which exception do you catch? D you use `Python 3.X`? – Rudziankoŭ Mar 23 '16 at 13:37
  • I don't even know exactly, but I guess I use Python 2. I use Thonny to write a code. – Martin Rehkli Mar 23 '16 at 13:39
  • execute please, `import platform; print(platform.python_version())` to get it know – Rudziankoŭ Mar 23 '16 at 13:43
  • Then everything should work fine, send me exception please if it isn't – Rudziankoŭ Mar 23 '16 at 13:57
  • If I'll try to run the script, then it says: >>> %Run inglisek.py File "C:\Users\Rehkli\Desktop\Python\inglisek.py", line 1 inserted_code = input("insert postcode") ^ SyntaxError: unexpected indent – Martin Rehkli Mar 23 '16 at 14:10
  • 1
    @MartinRehkli, strongly recommend you watch this video https://www.youtube.com/watch?v=Cd-k6QQMa18 otherwise you will hit this exception very often during you start of programming on Python – Rudziankoŭ Mar 23 '16 at 14:21
0

The easiest way is to match against special regular expression - template which postcode should match.

    inserted_code = input("insert postcode")
    import re
    if re.match('^[1-9][0-9]{4}$', inserted_code): 
       print("Estonian postcode")
    else:
       print("Not an estonian code")
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59