Question

In: Computer Science

Code is only reading the last line from the text files. How would I resolve? This...

Code is only reading the last line from the text files. How would I resolve? This is for the Name Search problem in Starting out with Python book. def main(): #open a file for reading infile = open('GirlNames.txt' , 'r') #Read the contents into a list Girls = infile.readlines() #open another file for reading infile = open('BoyNames.txt', 'r') #Read the contents into a lits Boys = infile.readlines() #Get a name to search for Name = input('Enter a name: ') #Determine if name is in list if Name in Girls or Name in Boys: print( Name, "is one of the popular names.") else: print(Name, "was not found in the list.") main()

Solutions

Expert Solution

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

CODE

def main():
   #open a file for reading
   infile = open('GirlNames.txt', 'r')
  
   #readlines will read all names into list with a newline character except last name
   #splitlines will read all names into list without newline character
  
   #Read the contents into a list
   Girls = infile.read().splitlines()
  
   #open another file for reading
   infile = open('BoyNames.txt', 'r')
  
   #Read the contents into a list
   Boys = infile.read().splitlines()
  
   #Get a name to search for
   Name = input('Enter a name: ')
  
   #Determine if name is in list
   if Name in Girls or Name in Boys:
       print( Name, "is one of the popular names.")
   else:
       print(Name, "was not found in the list.")
  
main()

OUTPUT

CODE SCREEN SHOT

TEXT FILE SCREEN SHOT


Related Solutions

How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
C Programing How would i edit this code to have it return the cipher text. void...
C Programing How would i edit this code to have it return the cipher text. void vigenereCipher(char* plainText, char* k){ int i; char cipher; int cipherValue; int len = strlen(k); //Loop through the length of the plain text string for(i=0; i<strlen(plainText); i++){ //if the character is lowercase, where range is [97 -122] if(islower(plainText[i])) { cipherValue = ( (int)plainText[i]-97 + (int)tolower(k[i % len])-97 ) % 26 +97; cipher = (char)cipherValue; } else // Else it's upper case, where letter range is...
I need to understand how I would edit this code without changing anything, only adding. I...
I need to understand how I would edit this code without changing anything, only adding. I am not looking for the exact answer, just information on how I would use the given code to complete some of the todo statements. Thank you! // include this header file so we can use `printf()` #include <stdio.h> // every C program must implement the `main()` function int main(int argc, char *argv[]) {    //TODO: check for enough arguments       // save the...
How would you define “business process management” based on this video and text reading? How would...
How would you define “business process management” based on this video and text reading? How would you compare it to business process re-engineering, continuous improvement, and total quality management approaches?
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Code in python Hello I want a program to read only the price of each line...
Code in python Hello I want a program to read only the price of each line in a txt file in python. The price is present right after the 2nd comma within the txt file. The output that I want is also printed at the bottom. Inside the txt file: 2019-08-01 00:08:39,BTC/USD,10128.08 2019-08-01 00:08:21,BTC/USD,10117.54 2019-08-01 00:08:20,BTC/USD,10120.51 2019-08-01 00:08:19,BTC/USD,10118.13 2019-08-01 00:08:18,BTC/USD,10120.74 Python file output: Price 1: 10128.08 Price 2: 10117.54 Price 3: 10120.51 Price 4: 10118.13 Price 5: 10120.74
How would I structure the following PHP (PDO) code into a table format using only PHP?...
How would I structure the following PHP (PDO) code into a table format using only PHP? //Our SQL statement, which will select a list of tables from the current MySQL database. $sql = "SELECT * FROM jobs"; //Prepare our SQL statement, $statement = $pdo->prepare($sql); //Execute the statement. $statement->execute(); //Fetch the rows from our statement. $tables = $statement->fetchAll(PDO::FETCH_NUM); //Loop through our table names. foreach($tables as $table){ //Print the table name out onto the page. echo $table[0], ' '; echo $table[1], '...
How can I fix this code to accomplish the goal of reading and writing on binary...
How can I fix this code to accomplish the goal of reading and writing on binary or text files? 3 import java.io.*; 4 import java.io.FileOutputStream; 5 import java.io.FileInputStream; 6 import java.util.Scanner; 7 8 public class ReadAndWrite implements Serializable 9 { 10 public static void main(String[] args) 11 { 12 boolean file = true; 13 Scanner inputStream; 14 PrintWriter outputStream; 15 FileInputStream inputBinary; 16 FileOutputStream readBinary; 17 FileInputStreamText writeText; 18 FIleOutputStreamText readText; 19 StringBuffer contents = new StringBuffer(); 20 Scanner keyboard...
I was not sure how to utilize this line because I made code that works but...
I was not sure how to utilize this line because I made code that works but not with this line specifically. C++ Function 2: bool exists_trio_within_distance(int*,int,int); //Input:    //an integer array (param 1), its size (param 2), and    //a distance (param 3) //Output:    //True or false //Behavior:    //Returns true is there exists    //a sequence of 3 values in the array    //such that sum of the first two elements    //is equal to the third element...
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT