~ Simplest way to search for a field in a file and return another field - teachyourselfpython


Search for a username in the file, and return the number of friends that that user has.

Challenge - Search for a username in file, and return the number of friends that the user has

Read the task instructions that are included in the comments in the code, and then get started!

You can either edit and code online in repl.it or alternatively, you can simply cut and paste the below into your own python file

Solve the challenge on repl.it

Code

""" ==============TASK
1. Search for any given username
2. Return the no. of friend's that the user has

e.g.
Enter username: marvR
>>marvR has 200 friends
"""

import csv

#1. This code snippet asks the user for their email address and returns their username

with open("fakefacebook.txt",newline="") as f:
  reader=csv.reader(f)
  email=input("Forgotten your username? Enter your e-mail and we'll help you:")
  for row in reader:
    for field in row:
      if field==email:
        print("Your username is:",row[0])
        

File Contents

Filename:fakefacebook.txt

username,password,email,no_of_likes
marvR,pass123,marv@gmail.com,400
smithC,open123,cart@gmail.com,200
blogsJ,2bg123,blog@gmail.com,99


Code your solution here

Systems Life Cycle (in a nutshell): Analyse - Design - Create - Test - Evaluate. Designing something or writing out some pseudocode before you actually write code is always a good idea! Get in to the habit of doing so! You can draw your flowchart here and screenshot it.

A sample flow chart (design) for this particular challenge could look like:

Flowchart: Python program to get the Fibonacci series between 0 to 50
Each challenge section below provides an online drawing tool where you can dynamically create flowcharts. Screenshot them into your presentation for submission.

Solutions & Answers

Answers /Solutions in the "members area" drive under: "Solve and Learn >>SOLUTIONS"

Testing Table

You may want to read a little about Testing first. A teacher may go through some examples with you. Feel free to fill in the test table here, and screenshot it in to your powerpoint. Testing is absolutely essential once you have created a program!
Test No. Description Test Data(input) Expected Outcome Actual Outcome Further Action?
1
2
3
4
5
Coming soon!