In [9]:
def extractDigits(num):
    d4 = num%10
    d3 = (num//10)%10
    d2 = (num//100)%10
    d1 = (num//1000)%10
    return (d1,d2,d3,d4)

def noRepeatingDigits(num):
    (d1,d2,d3,d4) = extractDigits(num)
    return not (d1 == d2 or d1 == d3 or d1 == d4 or d2 == d3 or d2 == d4 or d3 == d4)

# bulls and cows program. read user input 4-digit number with no repeats
# invalid input: (1) enter non-digits (2) number of digits is not 4 (3) no repeats
def readUserInput(gnum):
    s = input("Enter guess number "+str(gnum)+": ")
    while not s.isnumeric():
        print("Please enter only digits")
        s = input("Enter guess number "+str(gnum)+": ")
    num = int(s)
    while not noRepeatingDigits(num) or num < 1000 or num > 9999:
        print("Please provide 4-digit number with no repeats")
        s = input("Enter guess number "+str(gnum)+": ")
        while not s.isnumeric():
            print("Please enter only digits")
            s = input("Enter guess number "+str(gnum)+": ")
        num = int(s)
    return num  
In [10]:
readUserInput(5)
Please enter only digits
Please enter only digits
Please provide 4-digit number with no repeats
Please enter only digits
Please enter only digits
Please provide 4-digit number with no repeats
Please provide 4-digit number with no repeats
Out[10]:
1234
In [1]:
s = "1234"
t = "1w34"
print(int(s))
1234
In [2]:
print(int(t))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-970374d8aa40> in <module>
----> 1 print(int(t))

ValueError: invalid literal for int() with base 10: '1w34'
In [3]:
s.isnumeric()
Out[3]:
True
In [4]:
t.isnumeric()
Out[4]:
False
In [13]:
u = "ab1d"
In [14]:
u.isalpha()
Out[14]:
False
In [15]:
u.isalnum()
Out[15]:
True
In [16]:
int("231")
Out[16]:
231
In [ ]: