#๐Ÿ”’ New to python....displaying Items in list

38 messages ยท Page 1 of 1 (latest)

ruby craterBOT
#

@frigid lion

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

frigid lion
tranquil wagon
#

cat

frigid lion
#

this is the current output to give an idea of the goal. it should be the height of each instead of "10-12in" for both

jagged wolf
#

A problem with sharing screenshots is it is difficult to ctrl+f "compareTwoItems" in a screenshot.

mortal forge
#

you should really consider a dicti nstead of the two lists

#

a dict of dicts even

frigid lion
#

two lists is a requirement

mortal forge
#

ok

mortal forge
#

you need to get the indices of the breeds then, and use them in that function

#

!paste

ruby craterBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

jagged wolf
#

!code or this

ruby craterBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

mortal forge
#

!e

cats = ["meow", "purr", "nap"]
cat_stats = [[5, 1], [3, 2], [4, 0]]

print(cat_stats[cats.index("purr")])
ruby craterBOT
#

@mortal forge :white_check_mark: Your 3.12 eval job has completed with return code 0.

[3, 2]
frigid lion
#

that should work

mortal forge
#

do you see how you can use catData[cats.index("Main Coon")] to access that particular cat?

#

given that you got Main Coon from the user

frigid lion
#

in the print statement?

#

wdym

mortal forge
#

I'm not sure what your findIndex function is supposed to do

#

the iteration is pointless. if both cats are there it will return 0, otherwise it will return -1 after making the same check needlessly many times

#
  if index != -1:
      if comp1 in cats and comp2 in cats:
        # ...
  else:
      print("One or both of the cat breeds were not found.")

also, here you are doing the job of findIndex again

try this:

def compareTwoItems(comp1, comp2, cats, catData):
  a = "Adult Height"
  w = "Adult Weight"
  p = "Average Price"
  
  if comp1 in cats and comp2 in cats:
    cat1 = catData[cats.index(comp1)]
    cat2 = catData[cats.index(comp2)]
    print(cat1, cat2)
    print(f"{a:15}{w:15}{p:15}")
    data = input("What data would you like to compare?: ")
    if data == a:
       print(f"{catData[index][0]:20}{catData[index][0]:20}")
    elif data == w:
      print(f"{catData[index][1]:20}{catData[index][1]:20}")
    elif data == p:
      print(f"{catData[index][2]:20}{catData[index][2]:20}")
    else:
      print("Comparison not found (check casing)")
  else:
      print("Invalid cat breeds provided.")
#

cats.index(comp1) finds the index of comp1 in cats, then by calling catData[] on that index you find the stats of that cat

#

if you want to keep working with your own structure, you could make findIndex do the job of finding the indices of both cats

frigid lion
#

That does make sense

#

I get an error because 'index' is not defined

#

would that mean I'd have to make findIndex do that anyways?

mortal forge
#

!e

foo = ["a", "b", "c"]
print(foo.index("b"))
ruby craterBOT
#

@mortal forge :white_check_mark: Your 3.12 eval job has completed with return code 0.

1
mortal forge
#

I haven't used it much tbf, but I would expect your python should support it

#

it's ok if you want to look for it yourself though

ruby craterBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ New to python....displaying Items in list