PYTHON PROGRAMMING:
I have this program right now where it allows users to choose from a category(pulling from the file). Then it will print the University or people from that text file.
What I want to do next on my code is for users to search for a specific string from that file and it will display both the University and People that have that matching string. It can be the whole word or part of the string from that file.
Example:
search: ohn
output:
University: UCLA Name: John Kelly
University: U of Memphis Name: Taylor Johnson
Here is my current text file:
"UniversityName","ContactName" "UCLA","John Kelly" "UofFlorida","Mary Elizabeth" "U of Memphis","Taylor Johnson" "Harvard","Robert Fax"
This is what I've done so far with my code:
def load_data(file_name):
university_data=[]
with open(file_name,'r') as rd_file:
for line in rd_file.readlines():
line=line.strip().split(',')
T = line[0],line[1]
university_data.append(T)
return university_data
def main():
filename='List.txt'
university_data = load_data(filename)
print('[1] University\n[2] Contact Name\n[3] Exit\n[4] Search')
while True:
choice=input('Enter choice 1/2/3? ')
if choice=='1':
for university in university_data:
print(university[0])
elif choice=='2':
for university in university_data:
print(university[1])
elif choice =='3':
print('Thank You')
break
elif choice =='4':
print("Search Section Here")
else:
print('Invalid selection')
main()ANSWER CODE:
def load_data(file_name):
university_data=[]
with open(file_name,'r') as rd_file:
for line in rd_file.readlines():
line=line.strip().split(',')
T = line[0],line[1]
university_data.append(T)
return university_data
def find_name(university_data,sub_str):
for university in university_data:
res=university[1].find(sub_str)
if(res>0):
print('University:',university[0],'Name:',university[1])
def main():
filename='List.txt'
university_data = load_data(filename)
print('[1] University\n[2] Contact Name\n[3] Exit\n[4]
Search')
while True:
choice=input('Enter choice 1/2/3/4? ')
if choice=='1':
for university in university_data:
print(university[0])
elif choice=='2':
for university in university_data:
print(university[1])
elif choice =='3':
print('Thank You')
break
elif choice =='4':
sub_str=input("Enter any substring to search in
name:")
find_name(university_data,sub_str);
else:
print('Invalid selection')
main()
OUTPUT:

PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a...
I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...
I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...
Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...
Consider python please for this problem: I did a python program to read the first 20 lines from a file and here is the code holder = 20 lines = 3 file = "123456.pssm" _file = ".txt" while 1: out_file = input("[OUTPUT] - Please enter the name of file: ") + _file if (not _file): print("Filename not specified - Please Try Again\n") continue break with open(_file, "a") as e: with open(file, "r") as f: for num, line in enumerate(f, 1): ...
I am having a hard time with my program to assignment 4.12. Here
are the instructions:
The Payroll Department keeps a list of employee information for
each pay period in a text file. The format of each line of the file
is the following: <last name> <hours worked> <hourly
wage>
Write a program that inputs a filename from the user and prints
to the terminal a report of the wages paid to the employees for the
given period.
The report...
I am having trouble with my Python code in this dictionary and
pickle program. Here is my code but it is not running as i am
receiving synthax error on the second line.
class Student:
def__init__(self,id,name,midterm,final):
self.id=id
self.name=name
self.midterm=midterm
self.final=final
def calculate_grade(self):
self.avg=(self.midterm+self.final)/2
if self.avg>=60 and self.avg<=80:
self.g='A'
elif self.avg>80 and self.avg<=100:
self.g='A+'
elif self.avg<60 and self.avg>=40:
self.g='B'
else:
self.g='C'
def getdata(self):
return self.id,self.name.self.midterm,self.final,self.g
CIT101 = {}
CIT101["123"] = Student("123", "smith, john", 78, 86)
CIT101["124"] = Student("124", "tom, alter", 50,...
For my computer class I have to create a program that deals with
making and searching tweets. I am done with the program but keep
getting the error below when I try the first option in the shell.
Can someone please explain this error and show me how to fix it in
my code below? Thanks!
twitter.py - C:/Users/Owner/AppData/Local/Programs/Python/Python38/twitter.py (3.8.3) File Edit Format Run Options Window Help import pickle as pk from Tweet import Tweet import os def show menu():...
I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...
I have a python project that requires me to make a password saver. The major part of the code is already giving. I am having trouble executing option 2 and 3. Some guidance will be appreciated. Below is the code giving to me. import csv import sys #The password list - We start with it populated for testing purposes passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] #The password file name to store the passwords to passwordFileName = "samplePasswordFile" #The encryption key for the caesar...
PYTHON The provided code in the ATM program is incomplete. Complete the run method of the ATM class. The program should display a message that the police will be called after a user has had three successive failures. The program should also shut down the bank when this happens. [comment]: <> (The ATM program allows a user an indefinite number of attempts to log in. Fix the program so that it displays a message that the police will be called...