Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Answered Request for Help with List Methods in Python

Please find attached a screenshot of an error message pertaining to my code from an exercise in list methods in Python, specifically count() and index(). If anyone has any constructive criticisms or suggestions, I will be most grateful. Many thanks in advance for all your help.
 

Attachments

  • Screenshot_20220603-165650.jpg
    Screenshot_20220603-165650.jpg
    524.3 KB · Views: 2
Last edited:
Solution
Hey, So part of what didlly is saying is correct BUT this is not a INT this is a Float. Int would be 1 where as a float is 1.00. Best to get this sorted before you get too far along :D but lets first dig into whats happening here and why you are having issues :D


So the first bit you did :-
Python:
print(str(areas.index([-3])))

What you are doing here is trying to get option 3 from a list but its not a list and not coded correctly if you did want to get option 3 you would do this :-

Python:
print(areas[-3])

But this is not what you need, so lets move onto what it should be:-
Python:
print(areas.index(20.0))

nice and easy :) now the next bit:-
Python:
print(str(areas.count('9.50')))
although your code is correct it wont work. as...
Please find attached a screenshot of an error message pertaining to my code from an exercise in list methods in Python, specifically count() and index(). If anyone has any constructive criticisms or suggestions, I will be most grateful. Many thanks in advance for all your help.
Python:
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Print out the index of the element 20.0
print(str(areas.index(20.0)))

# Print out how often 9.50 appears in areas
print(str(areas.count(9.50)))

You use `.index(num)` on a list to get the index of that element in the list. Also for `.count()`, the variables in `areas` are floats, so make sure you do `.count(9.50)`, not `.count("9.50")`. Also there is no real need to convert the index and count to strings, but I left it there in case the challenge required the output to be a string.
 
Hey, So part of what didlly is saying is correct BUT this is not a INT this is a Float. Int would be 1 where as a float is 1.00. Best to get this sorted before you get too far along :D but lets first dig into whats happening here and why you are having issues :D


So the first bit you did :-
Python:
print(str(areas.index([-3])))

What you are doing here is trying to get option 3 from a list but its not a list and not coded correctly if you did want to get option 3 you would do this :-

Python:
print(areas[-3])

But this is not what you need, so lets move onto what it should be:-
Python:
print(areas.index(20.0))

nice and easy :) now the next bit:-
Python:
print(str(areas.count('9.50')))
although your code is correct it wont work. as soon as you put '9.50' you turned that into a string, but in your list it is a float so it will return nothing. But if you do :-
Python:
print(areas.count(9.50)
this will work. also just for the future if you do
Python:
print(type(areas))
this will tell you areas is a list, if you replace areas with 9.50 this will tell you its a float and if you do 9 this is a int and '9.50' is a string :)
 
Solution
Python:
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Print out the index of the element 20.0
print(str(areas.index(20.0)))

# Print out how often 9.50 appears in areas
print(str(areas.count(9.50)))

You use `.index(num)` on a list to get the index of that element in the list. Also for `.count()`, the variables in `areas` are integers, so make sure you do `.count(9.50)`, not `.count("9.50")`. Also there is no real need to convert the index and count to strings, but I left it there in case the challenge required the output to be a string.
Many thanks! Problem solved. (The second part of the exercise was correct on the first attempt.) :)
 
Hey, So part of what didlly is saying is correct BUT this is not a INT this is a Float. Int would be 1 where as a float is 1.00. Best to get this sorted before you get too far along :D but lets first dig into whats happening here and why you are having issues :D


So the first bit you did :-
Python:
print(str(areas.index([-3])))

What you are doing here is trying to get option 3 from a list but its not a list and not coded correctly if you did want to get option 3 you would do this :-

Python:
print(areas[-3])

But this is not what you need, so lets move onto what it should be:-
Python:
print(areas.index(20.0))

nice and easy :) now the next bit:-
Python:
print(str(areas.count('9.50')))
although your code is correct it wont work. as soon as you put '9.50' you turned that into a string, but in your list it is a float so it will return nothing. But if you do :-
Python:
print(areas.count(9.50)
this will work. also just for the future if you do
Python:
print(type(areas))
this will tell you areas is a list, if you replace areas with 9.50 this will tell you its a float and if you do 9 this is a int and '9.50' is a string :)
Many thanks! In particular, thanks for the step-by-step explanation. I applied this information successfully not only to the first part of the exercise (this one) but to the second part of the exercise on the first attempt. :)
 
Back
Top Bottom