Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Allow only digit penoxcz Unladen Swallow Posts: 3 Threads: 2 Joined: Nov 2017 Reputation: 0 Likes received: 0 #1 Nov-14-2017, 09:57 AM 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 ``` wavic So-and-so of the Yard Posts: 2,117 Threads: 28 Joined: Sep 2016 Reputation: 59 Likes received: 241 #2 Nov-14-2017, 10:42 AM 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 penoxcz Unladen Swallow Posts: 3 Threads: 2 Joined: Nov 2017 Reputation: 0 Likes received: 0 #3 Nov-14-2017, 01:22 PM (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 ``` wavic So-and-so of the Yard Posts: 2,117 Threads: 28 Joined: Sep 2016 Reputation: 59 Likes received: 241 #4 Nov-14-2017, 03:04 PM 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 « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Four digit combinations EHod 4 560 Aug-13-2017, 09:14 PM Last Post: EHod create a 20 digit string, and cast to a list then add all the digits as integers nikhilkumar 2 433 Jul-19-2017, 04:53 PM Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)