A simple Python Program
A simple Python Program, in this article i will demonstrate how to write a simple program using Python code.
About the sample Program
- The sample Program will have a list of the User Data Base and pass code
- The User is requested to enter the Pass code
- Then if the Pass code is correct
- The User will be ask to enter the Data
- If the Data is Correct , the Python Program will print out (“Data is correctly eneter ” ) through a Python Function
The Python Code
pins =["Brian":12345,"Ken":6789 ,"Lynn":abcd] def find_in_file(user_data) myfile = open("data.txt") readData =myfile.read() myfile.close() if user_data in readData: print("The data is Valid") else: return "No Such data" print("Please enter your Pass Code") userInput = int(input()) if userInput in pins.values(): print("Enter the data ") enterData = input() print(find_in_file(enterData)) else: print("Incorrect Pass Code") for key in pins.key(): print(key)
Stepping through the Code
- First ,I created a list to store the keys and value eg : Brian is a Key and 1234 is a value
- Then i created a function to check the data inside the File “data.txtx”
- The Function is pass a Parameter ” user_data”
- The function will open the “data.txt” file and by using the .read() function it will read the File and save what it reads into readData
- close the File to save memory
- Then check the “Argument ” pass into the Parameter ” user_data” and check whether the Argument Value the User enter is in the “data.txt” file
- If yes, tell the User that the data he or she entered is correct
- Else , tell them the Data they have enter is incorrect
- Now we come to the User interaction Part
- first ask the User to enter the Pass code value
- Check whether the User has enter a correct pass code value by checking through it using” pins.value “
- If the User enter the Correct Pin code Value, ask the User to input the Data
- Call the Function “find_in_file() ” and pass in the argument the User enter into the Parameter and check whether the User has entered the correct data
- else if the User enter the incorrect pass code, prints out the keys that are available to notified the User
Check out Writing and Reading File using Python here
Leave a Reply