#๐ How do i use the second thing from a dict list?
76 messages ยท Page 1 of 1 (latest)
@sand totem
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.
Hey @elder fossil!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
OP's data is only 1 dict. Not a list of dicts
no i want to use the second data to find the first
!rule ai is not why they're here for.
Basically find the value to find the key? Why not rearrange your dict to put the values as the key and the key as values?
that would be confusing with all of those 0
mainly because the rest of the values will be zero unless they have a another variable
0 is supposed to mean no button
essentially you can buy buttons
that's why there are empty buttons
yes
ok so you now have a different way of finding "equal"
that's exactly - what i am trying
You'll have to loop over the items and find it
if there really no other way
cuase this is a game
There are other ways for sure
yup
You could put it in a sqlite database
And then do a SQL ORM select where value clause
is that a libary?
!d sqlite3
Source code: Lib/sqlite3/
SQLite is a C library that provides a lightweight disk-based database that doesnโt require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. Itโs also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.
The sqlite3 module was written by Gerhard Hรคring. It provides an SQL interface compliant with the DB-API 2.0 specification described by PEP 249, and requires SQLite 3.15.2 or newer...
Its built-in to python
!pip sqlalchemy is handy library that makes queries easier to maintain and such
But tbh, its a bit of an overkill just for 1 button_placement. But if your game is going to grow and begins to hold more data, then the sqlite database makes a bit more sense
can you give me code or some video to understand how to use it with dictionaries
The doc I shared above shows a pretty decent guide on how to
Otherwise, you can try and search on youtube
Like this one https://www.youtube.com/watch?v=c8yHTlrs9EA
Are you a Python programmer who loves Data? This is your lucky video! Hit play as we dive into the world of SQLite in Python. We'll cover connecting & creating databases, cursors, different ways to execute queries, the secrets of dodging SQL Injection Attacks, and more! So buckle up, grab your coding gear, and let's get this party started!
Fin...
There are other less overkill ways like just using a variable to store that information so you dont need to recompute it every time
Why is "equal" in your first key value pair?
There multiple buttons in my calculator
Equal is one
I added as first for testing
Will that work for my use
hmm why are you using a dictionary instead of a list
valid point
let me test it out
if it words than I will end this thread if not than no
and why are you using multiple dictionaries instead of lists of objects (or you can use lists/tuples as grouped data)
cause I am retrying python after 2 years and the most coding I ever did was a simple weight calculator
in other words I am dumb
Yeah since the goal is learning python i would suggest practice with slightly better practices
List of dicts is useful as easy objects:
buttons = [{"x": 129, "y": 329...},...]
I would recommend it if you already know lists and dicts but havent learnt objects
boys thank you
i will try that later
It's done
what u need those 4 tuples of numbers for
show the whole code that is using those dictionaries tuples and the list
because from the little code that you showed so far it doesn't even looks like you are using the dictionaries tuples and the list for anything useful, and depending on how you're going to use the data would inform how the data would be better structured
they are tuples
i also thought they were {} at first, even then that would be a set tho
you're right, i was just stuck thinking of dictionaries after just reading through the whole thread from the top down as it mostly is about the dictionaries that OP originally used in their code
Ok
@kind fable
I essentially have to use replit cause my school require to usethe laptops they give us
and you can't install anything unless its from microsoft store (for exe file)
including pygame
and this is group work
so my partners needs to see this
i see, that's a pity, but using some online site works in that scenario
@sand totem about your code:
- you don't have to pre-populate your dictionaries like that when you initialize the dictionary, and you could probably do with just using lists instead in many scenarios
- the two loops in the beginning could be combined into one loop and eliminate a lot of code duplication
- creating the buttons would probably be better done in a loop to not need to repeat as much code and put that data into dictionaries instead
- the code could be simplified and shortened by reusing more code by encapsulating more of it in functions
- the code would greatly benefit from using classes to define your own data type to hold the data, but i don't know if that is something you have learned yet
and it would have been easier to read the code as text instead of a lot of screenshots, and as this is probably more code than would fit in one message here on discord, using this discord servers paste service is probably the best solution
the link to oaste service and the instructions about how to refer to your posted code here on discord will follow
!paste
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.
using text instead of screenshots doesn't only simplifying reading the code for others but it also makes it easier for people to provide help by not having to retype chunks of your code here in the chat to suggest changes or comment on parts of your code, increasing the chances of you receiving help here
This help channel has been closed. 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.