Question

In: Computer Science

This question has been done before but when i submit i receive a type ValueError on...

This question has been done before but when i submit i receive a type ValueError on line 9 where it says index_second_qoutes + 1. Could you please help or update code if needed. Thank U. (Code has been attached at the the end).

The votes are in… and it's up to you to make sure the correct winner is announced!

You've been given a CSV file called nominees.csv, which contains the names of various movies nominated for a prize, and the people who should be announced as the recipient. The file will look like this:

title,director(s)
Schindler's List,Steven Spielberg
"O Brother, Where Art Thou?","Joel Coen, Ethan Coen"
2001: A Space Odyssey,Stanley Kubrick
"Sherlock, Jr.","Buster Keaton, Roscoe Arbuckle"

You should write a program that reads in nominees.csv, asks for the name of the winning title, and prints out specific congratulations. For example, with the above file, your program should work like this:

Winning title: O Brother, Where Art Thou?
Congratulations: Joel Coen, Ethan Coen

Here is another example, using the same file:

Winning title: Schindler's List
Congratulations: Steven Spielberg
Code:

def main():
  film_director=[]
  with open('nominees.csv','r') as read_file:
    lines=read_file.readlines()
    lines=lines[1:]
  for line in lines:
    if '"' in line:
      index_second_quotes=line.index('"',1)
      index_third_quotes=line.index('"',index_second_quotes+1) This line gives the type Value Error
      title = line[:index_second_quotes].strip('\"')
      directors=line[index_third_quotes:-1].strip('\"').strip()
      film_director.append([title,directors])
    else:
      tokens = line.split(',')
      film_director.append([tokens[0].strip(),tokens[1].strip()])
   
  title=input('Winning title: ')
  for row in film_director:
    if title.strip()==row[0]:
      print('Congratulations:',row[1])
      break
main()

Solutions

Expert Solution

If you have any doubts, please give me comment...

It raises an error, if title doesn't have double quotes and directors have double quotes.

Code:

def main():

film_director=[]

with open('nominees.csv','r') as read_file:

lines=read_file.readlines()

lines=lines[1:]

for line in lines:

if '"' in line:

if line[0]=='"':

index_second_quotes=line.index('"',1)

index_third_quotes=line.index('"',index_second_quotes+1)

title = line[:index_second_quotes].strip('\"')

directors=line[index_third_quotes:-1].strip('\"').strip()

else:

index_first_quotes = line.index('"')

index_second_quotes = line.index('"', index_first_quotes+1)

title = line[:index_first_quotes-1].strip('\"')

directors = line[index_first_quotes+1:-1].strip('\"').strip()

film_director.append([title,directors])

else:

tokens = line.split(',')

film_director.append([tokens[0].strip(),tokens[1].strip()])

title=input('Winning title: ')

for row in film_director:

if title.strip()==row[0]:

print('Congratulations:',row[1])

break

main()


Related Solutions

needs to be done in C++ Q6. Coding Question (you must code this problem and submit...
needs to be done in C++ Q6. Coding Question (you must code this problem and submit via Blackboard): Keep everything as Integers. Row and Column Numbering starts at 1. Equation for each Cell :   Coll = (Row * Col * 10) Using Nested Loops display the following exactly as it is shown below:                                  Columns % Brand         1           2         3          4               A            10         20       30       40          B            20         40        60       80           C            30         60        90    ...
please do this in C++! I want to understand it, it must be done before the...
please do this in C++! I want to understand it, it must be done before the evening or nightime. Follow instructions exactly as it says. Please send a screenshot also with your code so I can see how it is supposed to be formatted. Since typing it a chegg answer, makes it look a bit messy. Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name...
This is Cost Accounting, I asked this question before, and when according to my professor some...
This is Cost Accounting, I asked this question before, and when according to my professor some of the answers were not correct, can you help me with this question please, thanks Decision Making – Equipment Replacement Mathews manages an assembly facility of Orthom Scientific. A supplier approaches Mathews about replacing a large piece of manufacturing equipment that Orthom uses in its process with a more efficient model. While the supplier made some compelling arguments in favor of replacing the 3-year-old...
Submit the following graphs. May be done in Excel and then paste them. Hospital A has...
Submit the following graphs. May be done in Excel and then paste them. Hospital A has 100 employees. Samples are as follows: Person 1 makes $1000 per week, Person 2 makes $2000 per week, Person 3 makes $3000 per week, person 4 makes $4000 per week and person 5 makes $5000 per week. Identify the type of graph and graph your results. Hospital B has 100 employees. All employees make $5,000 per week. Identify the type of graph and graph...
I have to submit a question and a solution to a problem for an assignment. Please...
I have to submit a question and a solution to a problem for an assignment. Please help! Select a single concept or topic covered in this course (e.g. regression, t-testing, ANOVA, correlation, etc) and create a supplemental material explaining it and its use. Aspects of your explanation may include: - When to use this statistical test. - How this statistical test functions - How this statistical test is computed - What are the assumptions of this statistical test? - Worked...
Use Excel to complete the following question. You must submit/attach all work done in Excel in...
Use Excel to complete the following question. You must submit/attach all work done in Excel in order to receive full credit. 5. A health advocacy group conducted a study to determine if the nicotine content of a particular brand of cigarettes was equal to the advertised amount. The cigarette brand advertised that the average nicotine content per cigarette was 1.4 milligrams. The advocacy group randomly sampled 24 cigarettes. The nicotine level for each of the sampled cigarettes is given below....
How is this done in R-studio? I have been trying to figure it out but I...
How is this done in R-studio? I have been trying to figure it out but I am getting more and more confused. While imprisoned by the Germans during World War II, the English mathematician John Kerrich tossed a coin 10,000 times and obtained 5067 heads. Let p be the probability of a head on a single toss. We wish to check if the data are consistent with the hypothesis that the coin was fair. a) Set up the hypotheses. Why...
Please type or submit a PDF document so that I am able to legibly understand the...
Please type or submit a PDF document so that I am able to legibly understand the answer. ( This is ome question 2. In the case study below you will explore the causes of B12 deficiency in three patients. A. Patient A is a college student who has become increasingly tired over the last six months. She thought it was because of her tough schedule but since she noticed tingling in her feet, she thought she should see a doctor....
So, here we go. I have never done this before. Sorry if I am violating any...
So, here we go. I have never done this before. Sorry if I am violating any rules. But I am stuck on several parts of a question. Only answered marked incorrect are incorrect. everything else is right. The question states: Bradley-Link’s December 31, 2018, balance sheet included the following items: Long-Term Liabilities ($ in millions) 11.0% convertible bonds, callable at 102 beginning in 2019, due 2022 (net of unamortized discount of $3) [note 8] $247 11.0% registered bonds callable at...
What is done to reduce the magnetic field inhomogeneities when installing the scanner and before acquiring...
What is done to reduce the magnetic field inhomogeneities when installing the scanner and before acquiring images? Describe briefly how these methods work.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT