Python Forum
Allow only digit - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Allow only digit (/thread-6288.html)



Allow only digit - penoxcz - Nov-14-2017

Hi, I need to make some check if num1 and num2 are digit then continue and if cislo_operace is digit then continue if not display custom error.
import re
print("Kalkulačka")
pokracovat = True
while pokracovat:
    prvni_cislo = input("Zadejte první číslo: ")
    druhe_cislo = input("Zadejte druhé číslo: ")
    num1 = re.sub('[^0-9]', '', prvni_cislo)
    num2 = re.sub('[^0-9]', '', druhe_cislo)    
    print("1 - sčítání")
    print("2 - odčítání")
    print("3 - násobení")
    print("4 - dělení")
    print("5 - umocňování")
    cislo_operace = input("Zadjte číslo operace: ")
    cislo = re.sub('[^0-9]', '', cislo_operace)
    if cislo_operace == ("1"):
        print("Jejich součet je:", int(num1) + int(num2))
    elif cislo_operace == ("2"):
        print("Jejich rozdíl je:", int(num1) - int(num2))
    elif cislo_operace == ("3"):
        print("Jejich součin je:", int(num1) * int(num2))
    elif cislo_operace == ("4"):
        print("Jejich podíl je:", int(num1) / int(num2))
    elif cislo_operace == ("5"):
        print(prvni_cislo, "na", druhe_cislo, "je:", int(num1) ** int(num2))
    else:
        print("Neplatná volba!")
    nezadano = True
    while nezadano:
        odpoved = input("\nPřejete si zadat další příklad? y / n: ")
        if (odpoved == "y" or odpoved == "Y"):
            nezadano = False
        elif (odpoved == "n" or odpoved == "n"):
            nezadano = False
            pokracovat = False
        else:
            pass
        
    



RE: Allow only digit - wavic - Nov-14-2017

The str class has method isdigit():

In [1]: x = 'faw23'

In [2]: y = '28539'

In [3]: x.isdigit()
Out[3]: False

In [4]: y.isdigit()
Out[4]: True



RE: Allow only digit - penoxcz - Nov-14-2017

(Nov-14-2017, 10:42 AM)wavic Wrote: The str class has method isdigit():

In [1]: x = 'faw23'

In [2]: y = '28539'

In [3]: x.isdigit()
Out[3]: False

In [4]: y.isdigit()
Out[4]: True

When I use this command it shows me this error: https://imgur.com/kf6T4Pn

import re
print("Kalkulačka")
pokracovat = True
while pokracovat:
    prvni_cislo = input("Zadejte první číslo: ")
    druhe_cislo = input("Zadejte druhé číslo: ")
    num1 = re.sub('[^0-9]', '', prvni_cislo)
    num2 = re.sub('[^0-9]', '', druhe_cislo)
    if num1.isdigit ():
    print("1 - sčítání")
    print("2 - odčítání")
    print("3 - násobení")
    print("4 - dělení")
    print("5 - umocňování")
    cislo_operace = input("Zadjte číslo operace: ")
    cislo = re.sub('[^0-9]', '', cislo_operace)
    if cislo_operace == ("1"):
        print("Jejich součet je:", int(num1) + int(num2))
    elif cislo_operace == ("2"):
        print("Jejich rozdíl je:", int(num1) - int(num2))
    elif cislo_operace == ("3"):
        print("Jejich součin je:", int(num1) * int(num2))
    elif cislo_operace == ("4"):
        print("Jejich podíl je:", int(num1) / int(num2))
    elif cislo_operace == ("5"):
        print(prvni_cislo, "na", druhe_cislo, "je:", int(num1) ** int(num2))
    else:
        print("Neplatná volba!")
    nezadano = True
    while nezadano:
        odpoved = input("\nPřejete si zadat další příklad? y / n: ")
        if (odpoved == "y" or odpoved == "Y"):
            nezadano = False
        elif (odpoved == "n" or odpoved == "n"):
            nezadano = False
            pokracovat = False
        else:
            pass
        
    



RE: Allow only digit - wavic - Nov-14-2017

You have if statement on line 9. At least the next line must be indented.