Question

Error in the 3rd line of code: TypeError: 'list' object cannot be interpreted as an integer...

Error in the 3rd line of code: TypeError: 'list' object cannot be interpreted as an integer

cityNames = []
cityNames = ['Providence', 'Denver', 'Miami', 'Chicago', 'Seattle']
  
for city in range(cityNames):

print(str(cityNames[city]))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Corrected code(this code is in python):

cityNames=[]

cityNames=['Providence','Denver','Miami','Chicago','Seattle']

#the error occured because range function takes a value to which

#the loop should iterate, here instead of that integer value, you have

#given the entire list itself

#for iterating through the entire list, instead of giving the list

#we should provide the length of the list, so that every element in

#the list could be accessed. Here len function is used to obtain

#length of the list cityNames and hence all elements in that list can be accessed.

for city in range(len(cityNames)):

    print(str(cityNames[city]))

Screenshot:

cityNames=[] 2 cityNames=[Providence, Denver, Miami, Chicago, Seattle] 3 #the error occured because range function

Output:

Your Output (stdout) 1 Providence 2 Denver Miami 3 4 Chicago 5 Seattle

Add a comment
Know the answer?
Add Answer to:
Error in the 3rd line of code: TypeError: 'list' object cannot be interpreted as an integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for python. I am getting the error, "TypeError: 'Series' objects are mutable, thus they...

    This is for python. I am getting the error, "TypeError: 'Series' objects are mutable, thus they cannot be hashed". (avgOPSdict is a dictionary, year is a list). Any help would be appreciated! How would I go about fixing this code? yearAvgOPS = [] for i in range (0, len(playerOPS)): x = avgOPSdict[year[i]] #this line is indented but chegg doesn't let me indent yearAvgOPS.append(x) #this line is indented but chegg doesn't let me indent print(yearAvgOPS)

  • What is wrong with my python code. I am receiving this error: Traceback (most recent call...

    What is wrong with my python code. I am receiving this error: Traceback (most recent call last): File "C:/Users/owner/Documents/numberfive.py", line 7, in if age > 18: TypeError: unorderable types: str() > int() My code is age= input("Enter your age:") if age > 18: print("You can vote.") else: print("You can't vote.") if age > 64: print ("You're a Senior Citizen...you deserve two votes") endofprogram = input("Please hit enter to exit from this program:")

  • Which line, if any, from the given code snippet would produce a compilation error? StackADT<object> mainList...

    Which line, if any, from the given code snippet would produce a compilation error? StackADT<object> mainList = null; // Line 1 mainList = new ArrayStack<T>(); // Line 2 mainList.push((T[]) (new Object[5])); // Line 3

  • Why is L75 different than the Third Quartile? I can easily find 3rd Quartile using excel....

    Why is L75 different than the Third Quartile? I can easily find 3rd Quartile using excel. I'm not sure how to find L75 though? Thank you!!   The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April 13, 2012). The average commute time in minutes for 48 cities are as follows: Click on the datafile logo to reference the data. DATA file Albuquerque 23.3 Jacksonville 26.2 Phoenix 28.3 Atlanta 28.3 Kansas City 23.4 Pittsburgh 25.0...

  • Please Help with the code below not sure where my error is...Thanks. Program output displayed here:...

    Please Help with the code below not sure where my error is...Thanks. Program output displayed here: Davy's auto shop services Oil change -- $35 Tire rotation -- $19 Car wash -- $7 Car wax -- $12 Enter the first service from the menu: Traceback (most recent call last): File "main.py", line 9, in choice1 = input("Enter the first service from the menu: "); EOFError: EOF when reading a line Here is the code: # Type your code here services =...

  • 1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer...

    1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer >(); ArrayList b = a; a.add(new Integer(4)); b.add(new Integer(5)); a.add(new Integer(6)); a.add(new Integer(7)); System.out.println(b.size()); A)1 B)2 C)3 D)4 E)5 2. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code:     Person p1, p2, p3;     int m1, m2, m3;     p1 = new Person();     m1 = p1.getMoney();     // assignment 1...

  • I am having trouble figuring out what is wrong with my code. This is the error...

    I am having trouble figuring out what is wrong with my code. This is the error I get for the last for loop. TypeError: list indices must be integers or slices, not list. This is the code: for line in fhr: ls = line.split(',') customer_list.append([ls[0], ls[1], ls[2], ls[3], ls[4], ls[5], ls[6], int(ls[7]), ls[8], ls[9], ls[10].strip('\n')]) from operator import itemgetter customer_list.sort(key=itemgetter(7), reverse=True) print(customer_list) writepath = './loan-data-output-v1.csv' fwh = open(writepath, 'w', encoding='utf-8') fwh.write('Name' +','+ 'State' +','+'Age' +','+'Annual Income'+','+   'Loan Type' +','+' Loan...

  • My program crashes on the line (print('\nThe maximum value from the file is {}'.format(max(i)))) with the...

    My program crashes on the line (print('\nThe maximum value from the file is {}'.format(max(i)))) with the error message TypeError: "int" object not iterable. The purpose of this program is to create an process binary files. Can you tell me what's going wrong? Thanks! ( *'s indicate indentation) from sys import argv from pickle import dump from random import randint from pickle import load if (argv[1] == 'c'): ****output_file = open(argv[2], 'wb') ****for i in range(int(argv[3])): ********dump(randint(int(argv[4]), int(argv[5])), output_file) ****output_file.close() ****print('{}...

  • I am using python3.6 and i am getting an error on line 42 num = int(fin.readline())...

    I am using python3.6 and i am getting an error on line 42 num = int(fin.readline()) #reading first value valueError: invalid literal, for int() with base 10 Any help is appreciated, here is the code. reads from txt file a few integers in an array and sorts them. thank you! # Function to do insertion sort def insertionSort(arr):     # Traverse through 1 to len(arr)     for i in range(1, len(arr)):         key = arr[i]         # Move elements of...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT