0

I was successful in creating a code for the PIN card question, but I am still lost on the logic. Can someone help? (Try & Except

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 CODE CREATED: pin = input() try: #your code goes here for i in pin: if not i == int(i): #print(not i == int(i)) # x= (int(i) + 0) x = "PIN code is created" print(x) except : #and here print("Please enter a number")

7th Oct 2021, 10:37 PM
Simon Varghese
Simon Varghese - avatar
6 odpowiedzi
+ 2
Simon Varghese # is use for comments in python so all the lines that has # in their starting are not read by the python interpreter & treated as a comment.
7th Oct 2021, 11:29 PM
zexu knub
zexu knub - avatar
+ 1
1. You're asking user input in the pin variable. 2. Try & except block for catching any errors if your input ended up being a character then instead of showing the error program will simply execute the except block where you have a error free message that says please enter a number. ( by error free i mean it doesn't look ugly on the console aside from that it is still an error).
7th Oct 2021, 11:19 PM
zexu knub
zexu knub - avatar
+ 1
Thanks!!!
8th Oct 2021, 10:54 AM
Simon Varghese
Simon Varghese - avatar
0
Thanks Zexu But can you pls break down the code that I created inside the TRY block( Avoid the codes that has the # key attached to it)
7th Oct 2021, 11:25 PM
Simon Varghese
Simon Varghese - avatar
0
Hi Simon! Instead of using for loop to check each characters one by one, you can try this pin = input() try: pin = int(pin) print("PIN code is created") except ValueError: print("Please enter a number")
8th Oct 2021, 1:22 AM
Python Learner
Python Learner - avatar
0
#Please find my code below and let me know your thoughts, Thanks :) try: pin = int(input()) print("PIN code is created") except: print("Please enter a number")
9th Aug 2022, 7:58 AM
Sait Nasif