#🔒 Dictionaries

54 messages · Page 1 of 1 (latest)

hexed pebble
#

Hello, to be honest first I wanted to ask how do I get better at this. I keep getting demotivated because it feels like if you don't have that problem - solving skill you will get nowhere in programming languages and second of all I am trying to solve this exercise, but I am stuck with the last part of the exercise.

Write a program that first reads two dictionaries articles1 and articles2 (see previous question).
To do this, use the function read_articles() from the previous exercise.
Now create the dictionary articles3 by merging articles1 and articles2.
Please note: For identical item names, the quantities must be added together.
Print the two read dictionaries and the merged dictionary.
Then print the merged dictionary sorted by article name.

stiff iceBOT
#

@hexed pebble

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.

hexed pebble
#

I am sorry it's in dutch

final nimbus
hexed pebble
#

def lees_artikels():
naam = str(input('Geef artikelnaam (<Enter> om te stoppen): '))
artikels = dict()
while naam!= '':
artikels[naam] =int(input('Geef aantal: '))
naam = str(input('Geef artikelnaam ( <Enter> om te stoppen): '))
return artikels

def print_artikels(artikell):
for key,value in artikell.items():
print(key,'{:10}'.format(value))

def main():
print('Lees artikels1: ')
artikel1 = lees_artikels()
print('Lees artikels2: ')
artikel2 = lees_artikels()
print('artikels1:',artikel1)
print('artikels2:',artikel2)
print('artikels1: ')
print_artikels(artikel1)
print('artikels2: ')
print_artikels(artikel2)

if name == 'main':
main()

stiff iceBOT
#

Hey @hexed pebble!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
still moat
#

Hello

hollow kite
#

Taking it away from programming first, if you had two box full of balls which steps would you take to merge them into a single box?
Like, if you were manually sorting these into a single box

hexed pebble
#
def leesartikels():
    naam = str(input('Geef artikelnaam (<Enter> om te stoppen): '))
    artikels = dict()
    while naam!= '':
        artikels[naam] =int(input('Geef aantal: '))
        naam = str(input('Geef artikelnaam ( <Enter> om te stoppen): '))
    return artikels

def printartikels(artikell):
    for key,value in artikell.items():
        print(key,'{:10}'.format(value))


def main():
    print('Lees artikels1: ')
    artikel1 = leesartikels()
    print('Lees artikels2: ')
    artikel2 = leesartikels()
    print('artikels1:',artikel1)
    print('artikels2:',artikel2)
    print('artikels1: ')
    print_artikels(artikel1)
    print('artikels2: ')
    print_artikels(artikel2)

if __name == '__main':
    main()
hexed pebble
#

so same colors with each other

#

or did I misunderstood your question?

hollow kite
#

as if you were giving instructions to someone, with one instruction per line

hexed pebble
#

idk how to break this down even more if you just want to sort it, you just bring the same colors together and then merge it into another box

hollow kite
#

Which operations do you have to execute?
In which order do you have to execute them?

#

Explain what to do in order to "bring the same colors together"

hexed pebble
#

well if the color of the ball you see is the same you add them together

hollow kite
#

as obvious as it might sound, you have to make your instructions as detailed as possible

hollow kite
hexed pebble
#

so if the color of the ball is the same as the color of another ball you add them together

#

and if not you just add it to the box

still moat
#

if you are struggling with solving such exercises, I would suggest to go to basics and re-learn the concepts of it. There is great book for it called - an introduction to programming using python.

when I was starting to solve problems on leetcode, I would struggle a lot, with the python language itself, I would know how to solve the questions in real world, like on paper, but not with code, and once I read the book and solved the tasks in the book, it easier.

so your question looks like one from leetcode. so thats my take for your question.

hollow kite
hexed pebble
#

so you have to look at the balls of the first box and the first box, and if you see that the ball or balls of the first box has/have the same color as the balls of the second box you add them together

hexed pebble
still moat
#

I see

hollow kite
hexed pebble
#

im actually still confused thoug

still moat
#

are the inputs int:int ?

final nimbus
#

and so on with all of the others

final nimbus
#

if u want i made a code example for your case

hexed pebble
#

I want to be able to understand how to think like it keeps demotivating me because I really don’t know how to think for programming exercises

still moat
final nimbus
#

thats where u get satisfaction

#

and get better

final nimbus
still moat
hexed pebble
#

I like this

final nimbus
#

ooh if its for an exam thats another topic then

hexed pebble
#

but I just feel like I am not improving

#

Even doing a ton of exercises alr

hollow kite
still moat
#

another thing that could help is writing down the inputs in paper and trying to solve it there, like step by step, thus you will have the solution on your head, and you can try to implement it to code

hollow kite
# hexed pebble so you have to look at the balls of the first box and the first box, and if you ...

so you have to look at the balls of the first box and the first box, and if you see that the ball or balls of the first box has/have the same color as the balls of the second box you add them together
instead of abstract instructions like "look at the balls", "see if ... has same color", "add them together", you should try to write more clear instructions like

  • Look at each ball in the first box and pick it up in your left hand
    • Look at each other ball in the first box and pick it up in your right hand
      • Compare if the balls in your hands have the same color
        • If so, put the ball in your right hand in the new box
        • Otherwise, put the ball in your right hand back where you got it from
    • Put the ball in your left hand in the new box
hexed pebble
hexed pebble
hollow kite
#

try to convert that to a function, then modify the instructions to take into account the second box, then adjust the function based on the new instructions (in that order)

(that way it's looking at the boxes in a way closer to lists of strings than dictionaries of string -> integers though)

stiff iceBOT
#
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.