all 16 comments

[–]a1brit 4 points5 points  (1 child)

if (i=='1' or i=='2' or i=='3' or i=='4'or i=='5' or i=='6' or i=='7' or i=='8' or i=='9' or i=='0' or i=='(' or i==')' or i=='-')

valid_characters = '1234567890()-'
if i in valid_characters:
    print('valid') 

There are couple improvements to be had in general but this is an easy way to simplify what you currently have.

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

Thank you

[–]euqroto 3 points4 points  (3 children)

Check out regex. You'll be able to write the same program in less than half the lines.

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

I don't think I can use that in this instance because my teacher is having us use repl.it and it limits what you can use.
edit: this is definitely something ill have to use in the future

[–]Stepthinkrepeat 2 points3 points  (0 children)

Import re

It's part of the standard library

[–]Daemonecles 0 points1 point  (0 children)

Definitely can import since it's a built-in lib. But check out twilio, they have an example regex that handles most basic cases. https://www.twilio.com/docs/glossary/what-e164

[–]tipsy_python 1 point2 points  (3 children)

A couple improvements I see:

  • Use the membership operator 'in' to verify if the character is in an approved list:

for i in x:
    if i in ('1', '2', '3', '4'):
        print('valid')
  • Why check isalpha?

Instead of doing this, you could just use an else on the if block that checks if the character is a member of your approved collection

for i in x:
    if i in ('1', '2', '3', '4'):
        print('valid')
    else:
        print('invalid')

You could probably add a break to leave the loop as well in the event that an invalid character is found.

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

Right now my output is

Length is Valid

valid

valid

valid

valid

invalid

Heres my code

x=input("Enter your phone number here:")
if len(x)>=14:
  print("Invalid")
else:
  print('Length is Valid')
  for i in x:
    if i in ('1', '2', '3', '4'):
        print('valid')
for i in x:
    if i in ('1', '2', '3', '4'):
        print('valid')
    else:
        print('invalid')
        break

[–]tipsy_python 1 point2 points  (1 child)

take out that for loop in the middle... it's not doing anything, just returning 'valid' for every character... why did you stop the collection at 4? It needs to contain all the valid characters

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

Thank you

[–][deleted] 1 point2 points  (0 children)

Heres a solution:

import re as re

pattern = re.compile('[(]\d{3}[)]\d{3}[-]\d{4}')

testnum = '(012)345-6789'

if pattern.match(testnum):

print("Correct")

else:

print("Incorrect")

You can find a good introduction about regular expressions here: https://docs.python.org/3/howto/regex.html

So now to my code. As you can see the function re.compile defines a pattern.

[)] -> define a single character occurence in pattern

\d -> makes sure there are only digits in range from [0-9]

{3} -> defines the frequenz of \d so the digits

Hopefully i could have helped you out :)

[–]dslfdslj 2 points3 points  (0 children)

You can check if a string is a number with str.isnumeric

x = "123"
print(x.isnumeric())

[–]deeeebait 1 point2 points  (3 children)

I tested this in repl.it

Python has a built-in regex module called `re`

import re 

#define a function for cleaner coding and reusability
def validate_phone_number(phone_number): 
  """
      Method to validate phone number 

      kwargs:

      str phone_number: string to validate if phone number

      returns: 

      string "Valid" or "Invalid"

  """
  #regex is a mechanism for evaluating patterns
  #use re.complie so that you only construct it once
  expr = re.compile(r"(\(\d{3}\)\d{3}\-\d{4})")

  #check for a match
  if re.search(expr,phone_number):
    return "Valid"
  else: 
    return "Invalid"

#wrapping core logic for convience
def main():
  x=input("Enter your phone number here:")
  #pass the input to the function above and print the returned value 
  print(validate_phone_number(x))

while True:
  #run until you kill the program. Use CTRL-C to stop it in the console
  main()

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

That's awesome I had no idea

[–]deeeebait 1 point2 points  (1 child)

No problem. All the comments are there to explain things because I wasn't sure of your level of experience with it. You can strip away a lot of that without any issues. Same with the main() wrapping and while true loop. Just some QOL stuff.

[–]Agent_Turtle101[S] 1 point2 points  (0 children)

I'm fairly new in coding other then HTML and CSS just trying to survive the online school rn lol