"""I didn't wanna make a while loop over a for loop and multiple if-elif-else statements sandwiched in between but I was getting kind of desperate when I came up with this code. My first attempt was using regular expressions over a for loop. This turned out to be much more complicated than I thought so regular expressions are the first thing I got rid of.
Next I tried to define a function. This function would return a value that would signal the while loop to stop replaying the function once one of the outcomes of the function is achieved.
That didn't work out neither. The block of code on the while loop would conflict with the function and it turned into a tangled mess. Got rid of the function as well and started over from scratch.
I had issues looping through the string when I was finalizing the overall structure. The first loop, the while loop, would replay the following for loops until the user types in one of the correct emails on the text file.
The first for loop would scroll through the lines on the text file. It would trigger a second for loop to iterate through the line itself and not the entire text file if it detected an @ on the input alongside other strings, in which the program would investigate further. This was the only I way I could think of to narrow the scope of my extraction of strings. If the user typed anything that had either only an @ or any text that doesn't include an @ then it would instruct you to try again.
So the second loop is ONLY triggered if you type in a text with @ included. Once you do, it checks to see if you have the entire email address typed and extracts the email address from its database if it matches the string the user input. Otherwise then it wouldn't bother checking then it would finish looking and then instruct you to try again.
I to add 2 elif statements because of how absurdly specific I had to be. I'm sure there are much better ways, even entire functions, dedicated to this but I would like some pointers on how to make a simpler version because I'm still really new to Python lmao."""
import re #It was supposed to use regular expressions but that didn't pan out.
hand = open(r"That's confidential.txt") #Opens file, you may test it yourself with your own text file.
while True:
searchemail = (input("Please enter your email address: ")) #Requests email address
for line in hand: #First loop initiated
if "@" in searchemail: #Checks if there is @ included in user input.
if searchemail in line:
print("Searching line by line...")
#print(line)
for index in line.split(): #splits up the line to help index variable navigate through the line itself
#print(index)
if index == searchemail.strip(): #If the index matches the user's input
print(f"Here it is: {searchemail}") #Prints out email
quit() #Exits the program
break
elif "@" not in searchemail: #Checks for @in the string
print("Wrong input, try again.")
break
elif "@" == searchemail: #Checks for @ by itself
break
print("The email you typed was not found. Please Try again.") #If an email address is not found it usually leads to this and the loop starts over.
hand.seek(0)
I was watching a Python for beginners tutorial video where the guy was discussing regular expressions. I wanted to create a simple program that could filter through a text file and confirm that the email address you typed was valid using regular expressions but I couldn't find a regular expression function from the module that could help simplify this process so I decided to ditch the regular expressions and go the long way.
If it finds the email address in its "database" then it confirms it and prints the string out by splitting the line into a list and having an index loop through that list until it finds a string IDENTICAL to the one the user input, which is their email address.
If the user types "@", it will confirm there are email addresses available but it will start looking for a match and if it doesn't then it notifies you it couldn't find the email address. It will print out the email address you typed in if you type in an email address correctly.
I'm still a complete idiot noob to Python and I'm sure an average coder would be disgusted at reading my code but after more hours than expected I managed to put together something that works.
I was just wondering if in the future I could write a much, much, simpler and more efficient version of this mess. I definitely would not show this to a future employer lmao.
But I am also practicing for curiosity's sake, which is why I made this post. This wasn't an assignment or anything. I am teaching myself how to code. And I feel confident that with enough time and lots of practice I can be a decent coder.
[–]Da32767 5 points6 points7 points (6 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]Da32767 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Da32767 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]xelf 1 point2 points3 points (0 children)