-1

I'm new to coding and this is my first question here! I'm trying to do some automation tools and in my first step i want to get data from the user and validate it.

I'm trying to get domain name from user and check the TLD if its '.co.il' keep to other task if not print a msg.

I read and try few things but nothing work for me.

This is my last try code:

domainBuy = input('Please Enter Domain Name: ')

def domaincheck(domainBuy):
    if '.co.il' in domainBuy:
        print('DOMAIN GOOD!')
    else:
        print('bad')

I will appreciate any tips! Thanks!

Alan Bagel
  • 818
  • 5
  • 24
Shay Amos
  • 17
  • 2

3 Answers3

1

Since Domain names are at the end of URL, it would be better to use endswith().

x.endswith('.co.il') - Returns True if x ends with .co.il else returns False

domainBuy = input('Please Enter Domain Name: ')
def domaincheck(domainBuy):
    if domainBuy.endswith('.co.il'):
        print('DOMAIN GOOD!')
    else:
        print('bad')

# calling your function
domaincheck(domainBuy)
Ram
  • 4,724
  • 2
  • 14
  • 22
0

It's better to define your functions first, and then call them I'm guessing the confusion comes from using the same variable name in the function as for your input, maybe this will make it clearer?

def domaincheck(domainBuy):
    if '.co.il' in domainBuy:
        print('DOMAIN GOOD!')
    else:
        print('bad')

domain_name = input('Please Enter Domain Name: ')
domaincheck(domain_name)

As Barmar points out in the comment, domainBuy.endswith('co.il') is a better test.

thebjorn
  • 26,297
  • 11
  • 96
  • 138
0

Although slightly more complex, regular expressions are usually used when validating input patterns and can apply to a much wider range of applications in the event endswith isn't sufficient.

Example:

import re

def domaincheck(input_str):
    pattern = re.compile(r'\.co\.il$')
    if pattern.search(input_str):
        print("Good")
    else:
        print("Bad")

domaincheck('test.co.il')
domaincheck('test.co.il.invalid.com')

Output:

Good
Bad

Update:

To address your question regarding looping until a valid URL is entered:

import re

def domaincheck(input_str):
    pattern = re.compile(r'\.co\.il$')
    if pattern.search(input_str):
        print("Good")
        return True
    else:
        print("Bad")
        return False

while True:
    user_input = input("Enter URL: ")

    if domaincheck(user_input):
        break
Abstract
  • 985
  • 2
  • 8
  • 18