Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Allow only digit
#1
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
        
    
Reply
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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
        
    
Reply
#4
You have if statement on line 9. At least the next line must be indented.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,496 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,787 May-28-2020, 09:50 AM
Last Post: bryan0901
  Four-digit number text translation. soz 3 2,651 May-13-2019, 03:02 PM
Last Post: buran
  create three digit numbers Krszt 4 4,399 Dec-09-2018, 03:12 PM
Last Post: ThePhi
  Sum of digit in a string MEH012 1 9,333 Apr-20-2018, 02:13 AM
Last Post: Larz60+
  Four digit combinations EHod 4 7,704 Aug-13-2017, 09:14 PM
Last Post: EHod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020