Python Exception Handling practice | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python Exception Handling practice

Hi everyone, I'm trying to get this code to give an error for anything that isnt an integer but its not working, could anyone help? This is the question - We need to create a program that allows users to create their own PIN codes for their bank cards. Each PIN code consists of digits. Complete the program so that when the user enters a character, the program stops and outputs "Please enter a number" and when the user enters only digits, the program outputs PIN code is created". Sample Input 44a5 Sample Output Please enter a number My code is: pin = input() try: PIN=int() print('PIN code is created') except: PIN!=int() print('Please enter a number') It works for the valid PIN number entries but still prints PIN code created for the entries that contain strings. Thanks

8th Aug 2021, 2:49 PM
Sean Taylor
Sean Taylor - avatar
3 Answers
0
Thanks alot. That worked and makes sense> I left int() blank where I needed to type pin in the brackets then the expect statement handled any other type of input. great !!
8th Aug 2021, 3:06 PM
Sean Taylor
Sean Taylor - avatar
0
By my dear friend @Muhammad Sufyaan Hafs. pin = input() try: pin = int(pin) print("PIN code is created") except ValueError: print("Please enter a number")
9th Aug 2022, 1:02 PM
Munasar Muhidin
0
def withdraw(amount): print(str(amount) + " withdrawn!") #your code goes here pin = input() try: pin = int(pin) withdraw(pin) except ValueError: print("Please enter a number")
6th Nov 2023, 9:24 AM
Jens-Andreas Hoheisel
Jens-Andreas Hoheisel - avatar