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


Messages In This Thread
Allow only digit - by penoxcz - Nov-14-2017, 09:57 AM
RE: Allow only digit - by wavic - Nov-14-2017, 10:42 AM
RE: Allow only digit - by penoxcz - Nov-14-2017, 01:22 PM
RE: Allow only digit - by wavic - Nov-14-2017, 03:04 PM

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