Chapter 10 : Exception Handling In Python (Practice Sets)
जब Beginner Python Coders Python मे Codes लिखते है तो लगभग हर Beginner को किसी न किसी Error का सामना करना पडता है क्योंकि वे Python Exception Handling से संबंधित Problems को Solve नही करते है जिस कारण वे Python Codes लिखते Time बहुत - सी Errors को face करते है | आपको Exceptions से संबंधित कोई Problem ना हो हम ने इस Purpose से इस Web Page पर Problems Add की है | दि गई इन Problems को Solve करने से आप Python Exceptions के छोटे - से - छोटे Topic समझ पाएगे |
Problem 1 :
किसी Person के दवारा Contact Book मे Mobile Number के अलावा कुछ और enter करने पर Mobile Number save नही होता है | एक Program लिखे जो ,
- user से input मे user का mobile Number ले |
- Mobile number मे Numbers के अलावा कोई और value होने पर आने वाले error को handle करे |
Code Explanation
- try: = > try Block का उपयोग किया गया है |
- mobile_number = int(input("Enter Your Mobile number: ")) = > mobile_number एक variable है जो input मे integer value को store करता है |
- except ValueError: = > try block मे error आने के बाद except block चलेगा |
- print("Please Use Only Numbers") = > except block True होने पर ये print Exception Handling run होगा |
- else: = > except block मे error न आने पर else Block मे लिखा code run होगा |
- finally: = > Program को run करने पर finally block run होगा |
Problem 2 :
किसी Website , Mobile App etc. पर User के दवारा Negative Age enter करने पर User Account create
नही कर पाता है |
एक Program लिखे जो ,
- User से User की Age ले |
- User के दवारा Negative Age Enter करने पर आने वाले error को handle करे |
Code Explanation
- try: = > Program मे error आने की expectation मे Program को try block मे लिखा जाता है |
- age = int(input("")) = > age variable input मे integer value को store करता है |
- if age < 0: age variable मे stored value 0 से छोटी होने पर if statement True होगी |
- else: = > if statement के false होने के बाद else block चलेगा |
- except ValueError: = > try block मे आए error को excep block accept करता है |
- print("") = > ये print() Exception Handling try block मे आने वाले error को लिखे गए message से show करता है |
Problem 3 :
किसी calculator का उपयोग करते Time किसी Number को 0 से Devide करने पर कोई Result नही मिलता
है |
एक Program लिखे जो ,
- User से कोई दो Numbers ले |
- आपस मे user के दवारा दिए गए Numbers को Devide करे |
- User से किसी Number को 0 से devide करने पर आने वाले ZERODEViSION error को Handle करे |
Code Explanation
- try: = > error आने की expectation मे Program को try block मे लिखा गया है |
- x = int(input(" ")), y = int(input(" ")) = > x और y दो variable है जो input मे integer value को store करते है |
- result = x / y = > result एक variable है जो x और y की devision value को store करता है |
- print("Result :", result) = > print Exception Handling result variable को print करता है |
- except ZeroDivisionError: = > try block मे ZeroDivisionError आने पर except block चलता है |
- print("Error : Can not devide by 0") = > except block चलते ही print मे दिया गया message show होता है |
Problem 4 :
Atm के उपयोग से ऐसी Ammount निकालने की कोशिश करना जो व्यक्ति के Account मे stored Ammount से बडी है तो ATM उस Transaction को reject कर देता है | एक Program लिखे जो ,
- User से एक Ammount ले |
- User की enter की गई Ammount stored Ammount से कम या equal Ammount होने पर message show करे "Your Transaction completed"
- User की enter की गई Ammount stored Ammount से big Ammount होने पर आने वाले error को handle करे |
Code Explanation
- stored_ammount = 500 = > stored_ammount एक variable है जिसमे किसी व्यक्ति का bank balance stored है |
- withdraw_ammount = int(input(" ")) = > withdraw_ammount एक variable है जो user से input मे integer value को store करता है |
- if withdraw_ammount > stored_ammount : = > withdraw_ammount stored_ammount से बडी होने पर ये if statement True होती है |
- print("Transaction not successful") = > if statement के True होने पर ये print Exception Handling चलता है और screen पर Transaction not successful show करता है |
- else = > If statement के false होने पर else statement True होती है |
Problem 5 :
बहुत सी Websites , Apps , software , offline Forms मे Password column को खाली रखकर
आगे के steps की और जाने की कोशिश करने पर या Password column को खाली रखने पर
Forms को submit करने की कोशिश करने पर Form reject हो जाता है |
एक Program लिखे जो ,
Password Variable को खाली रखकर आगे के steps की और जाने की कोशिश करने पर
आने वाले error को handle करे |
Code Explanation
- try: = > error आने की expectation मे Program को try block मे लिखा गया है |
- password = input("Enter your password: ") = > password एक variable है जो input मे integer value को store करता है |
- if password == "": = > password variable के खाली होने पर ये if statement True होती है |
- print("Error: Password empty") if statement के True होने पर ये print Exception Handling चलता है और Error: Password empty message को show करता है |
- else: = > if statement के false होने के बाद else statement चलती है |
- print("Form submitted") else statement के True होने पर ये print Exception Handling चलता है और Form submitted message को show करता है |
- except Exception as Error: = > try block मे error आने के बाद except block चलता और try block मे आए errors को Error variable मे store करता है |
- print("Error:", Error) = > ये print Exception Handling Error variable मे stored value को show करता है |
Problem 6 :
नीचे एक Program दिया गया है जो ,
- user से Celcius मे Tempreture लेता है |
- user के दिए गए Celcius को Fahrenhiet मे convert करता है |
लेकिन इस Program को Run करने पर Logical Error आ रही है यानि Program काम कर रहा है लेकिन Wrong Fahrenhiet value देता है | आपको Program से उस Logical Error को खतम करके right Program लिखना है |
Code Explanation
- Celsius = float(input("Enter Temperature in Celsius: ")) = > Celcius ए क variable है जो input मे float value store करता है |
- Fahrenheit = (Celsius - 32) * 5/9 = > Fahrenheit एक variable है जो Celcius Tempreture को Fahrenheit मे convet करने की value store करता है |
- print("Fahrenheit :", Fahrenheit) = > print Exception Handling Fahrenheit variable मे stored value को show करता है |
Problem 7 :
पानी के जमने (freezing) और उबलने (boiling) के बिंदु को मापने का एक पैमाना Fahrenhiet है जिसमे
पानी 32°F पर जमता है और 212°F पर उबलता है |
नीचे दिए गए Code editor मे एक Python Program लिखे जो ,
- User से Fahrenhiet Tempreture ले |
- Fahrenhiet Tempreture को Celcius Tempreture मे convert करे
- Number value के अलावा कोई और value enter करने पर आने वाले error को handle करे |
Code Explanation
- Fahrenheit = float(input(""")) = > Fahrenheit एक variable है जो input मे float value store करता है |
- celcius = (Fahrenheit - 32) * 5/9 = > celcius एक variable है जो Fahrenheit Tempreture को celcius मे convet करने की value store करता है |
- print("", celcius) = > print Exception Handling celcius variable मे stored value को show करता है |
- except ValueError: = > except block try block मे आए error को except करता है |
- print("") = > ये print Exception Handling try block मे आए error को show करता है store किए गए print Exception Handling मे message से
Problem 8 :
Online Result को show करने से पहले सभी students का Result roll number के according store किया जाता
है |
एक Python Program लिखे जो ,
- user से उसका roll number ले |
- पहले से stored data मे user का roll number न मिलने पर आने वाले error को handle करे |
Code Explanation
- num = int(input("Roll No. 2")) = > num variable Input मे integer value store करता है |
- print(num) = > print() Exception Handling num variable को display करता है |
- except ValueError: = > try block मे 2 के अलावा कोई और value enter करने पर आने वाले error को handle करता है |
- print("Absent") = > try block मे आने वाले error को print Exception Handling मे लिखे गए message show किया गया है |
You have completed This Chapter ! 🎉
Now you can choose any one of the following options to test your knowledge :
Help Center
Users and we all help you together .