Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,150,224 members, 7,807,750 topics. Date: Wednesday, 24 April 2024 at 06:28 PM |
Nairaland Forum / Science/Technology / Programming / Validation Statement In Python (720 Views)
How To Create Mysql Database/python Tkinter Gui To Change Password/Validation En / Who Wants To Learn Programming In Python Easily Without Stress? READ THIS / URL For Validation During Remita Integration; How To Set It Up (2) (3) (4)
Validation Statement In Python by octaflav: 6:39am On Sep 30, 2021 |
Write a python code or algorithm that validate a student Reg No. It must start with two letters followed by four digits example : CK1234. An error will be flagged If a user enters an input that is not within the specified conditions. Kindly help me out with this. |
Re: Validation Statement In Python by tempest01(m): 6:44am On Sep 30, 2021 |
I am still new to python, but i think you would utilize the input() - to get user input try.....except....finally / or if else - to validate user input isalpha() & isdigit() in the try block to validate the alphabet and digit part of the input Modified - This should give you an idea of the step to take. I have not tested this and don't know if the code used is correct. Reg = input("Please enter your Reg Number: " if Reg[:1].isalpha() and Reg[2:5].isdigit(): #can also add another condition here to check if the length of input ==5 (and len(Reg) == 5) print("Reg Number Valid!" else: print("Not valid, You have to figure a way to loop the process." #Can use the while before the if/else to loop edited again. i think it is cleaner to use while for validation Reg = input("Please enter your Reg Number: " while not Reg[:1].isalpha() and Reg[2:5].isdigit() and len(Reg) == 5: #can try isnumeric() if isdigit doesn't work Reg = input("Error: Re-enter correct Reg number: " 2 Likes |
Re: Validation Statement In Python by octaflav: 12:20pm On Sep 30, 2021 |
tempest01: Thank you for taking out time to respond to this. I'll try the suggested approaches and get back to you. |
Re: Validation Statement In Python by bedfordng(m): 11:26am On Oct 01, 2021 |
octaflav: This should help while True: regn = input('enter reg nunber: ').strip() print(regn[:2], regn[2:]) if regn[:2].isalpha() and regn[2:].isnumeric() and len(regn) == 6: print("beta pikin" break continue |
Re: Validation Statement In Python by tempest01(m): 10:38am On Oct 02, 2021 |
octaflav: See below. Tested and working. 2 Likes
|
Re: Validation Statement In Python by octaflav: 9:05pm On Oct 02, 2021 |
tempest01: Good. I have entered the codes as is: here is the attached code and the result and the result I get each time I entered a reg no
|
Re: Validation Statement In Python by tempest01(m): 10:58pm On Oct 02, 2021 |
octaflav: Check your code. After "while not" should be "Reg" not "Re". If you read the error message, you should always identify the problem. Also, maybe it is just my eyes, but the "Reg" under "while not" the space is too much. Should press "tab" once. 1 Like |
Re: Validation Statement In Python by Deicide: 11:55pm On Oct 02, 2021 |
I think using regular expression is well suited for this kind of task 1 Like |
Re: Validation Statement In Python by Nobody: 12:01am On Oct 03, 2021 |
Hopefully programming section starts to seem more like stackoverflow. Nice work 1 Like |
Re: Validation Statement In Python by Yeyeboi05(m): 9:55pm On Oct 03, 2021 |
Nice.. Started my programming journey,August,it’s been easy-going so far but the only issues I’m having at the moment is loops..I guess with time,I’ll master! 1 Like |
Re: Validation Statement In Python by octaflav: 4:30pm On Oct 08, 2021 |
tempest01:Wow! It worked! I appreciate your persistence and invaluable contributions. Thanks a lot. 1 Like |
(1) (Reply)
I Want To Learn How To Connect Html Form With Mysql Database For A Fee / Mysql Guru And Php / Related To Html And Java Script?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19 |