#πŸ”’ Cant seem how to do this.

91 messages Β· Page 1 of 1 (latest)

vague cairn
#

How can i convert the values of a dictionary into list without the keys ?

resources_list = list(resources.items())
for amount in range(0,3):
    print(amount)
    resources_list_new = [resources_list[amount]]
    print((resources_list_new))

ive tried this but it converts it with the keys aswell.

ashen slateBOT
#

@vague cairn

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.

austere flume
vague cairn
#

thank. much appreciated

#

What about this error? AttributeError: 'list' object has no attribute 'split'

#

It appears when i run this code.

#

for amount in range(0,3):
resources_list_new.append([resources_list[amount]])
print(type(resources_list_new[0]))
resources_list_new = resources_list_new.split(' ')
resources_list_new = [int(n) for n in resources_list_new]
print(resources_list_new)

austere flume
#

its already splitted

vague cairn
#

Im trying to split the strings in the list to convert them into an integer.

austere flume
#

resources_list_new is already a list

#

you're saying you want to split each element in the list further?

vague cairn
#

Basically im just trying to convert the numbers inside the list into integers

austere flume
#

Yeah so you don't need to split() the list again

#

Do you understand? @vague cairn

vague cairn
#

Yeah

#

I tried to use because it worked before, but didnt realize that it was on a variable. My bad

#

So hod can I convert the str inside the list into integers?

austere flume
#

I'm saying you don't need to split it again before that line

vague cairn
#

ohhh

#

Thank you bro

#

But then it gives this error. TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

austere flume
vague cairn
#

Yeah the probelm is the data is stored like this for some reason. [[300], [200], [100]]

#

How can i remove the extra brackets ?

austere flume
#

might be a wrong way to go about it. What do you wanna do with the numbers inside the list? Do you want to total them or something?

vague cairn
#

Im trying to add it with a user input

austere flume
#

Like if the user input is 10, you would 300 + 10?

vague cairn
#

yeah

austere flume
#

Can you show me a sample of your resources dictionary?

#

The way you're doing this is very... farfetched

vague cairn
#

resources = {
"water": 300,
"milk": 200,
"coffee": 100,
}

austere flume
#

ok that makes sense

#

So if you wanna update a value in your resources by 10, you just add to it by its key

vague cairn
#

yeah ive tried it before but it didnt work

austere flume
#
resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}
print(resources["water"])

This is how you access the value via its key

#

Dang forgot the !e

#

!e

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}
print(resources["water"])
ashen slateBOT
vague cairn
#

oh

austere flume
#

So that returns the value

#

So if you wanna "update" that value, you reassign it with the same key

vague cairn
#

so what about if i wrote it like this print(resources)["water"]

austere flume
#

!e

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}
print(resources["water"])
resources["water"] = resources["water"] + 10
print(resources["water"])

Here I'm assuming the user input "10" to add to water resource

ashen slateBOT
vague cairn
#

hmm

#

But the resources get updated when a user places the order, so its not always 300, 200,100 etc.

austere flume
#

!e

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}
print(resources["water"])
resources["water"] = resources["water"] + 10
print(resources["water"])
print(resources)

And of course that does also update the overall resource dict

ashen slateBOT
austere flume
vague cairn
#

let me try this one sec.

#

How can i loop through the dictionary to add them ?

#

for addition in range(0,length_refill + 1):
resources["water"] + refill_amount_list[addition]

#

i have it like this but my problem is that i do not know if i can loop through the values and change them

austere flume
#

!e

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}

def make_latte(res):
  """Take 1 water, 1 coffee and 1 milk to make a latte"""
  res["water"] -= 1
  res["coffee"] -= 1
  res["milk"] -= 1
  return res

resources = make_latte(resources)
print(resources)

Having fun here

ashen slateBOT
austere flume
vague cairn
#

yeah the input prompt goes like this. refill_resources = (input("Input the amount to refill in this order 'water','milk','coffee' or type 'cancel' to cancel refill: "))

austere flume
#

So you want to refill only one resource?

#

Can you start using code format blocks? I've misread your code here

#

!code format block below

ashen slateBOT
#
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.

vague cairn
#

No, the prompt asks the user to input the amount to refill for each resources, but for example if i want to refill the water and milk only ill input "100 100"

austere flume
#

Can they refill up to 99999999999 per resource?

vague cairn
#

yes 500ml , 400m, 200 grams

austere flume
#

400m?

vague cairn
#

ml*

austere flume
#

ok

#

Why not just reset the values to their max?

#
def refill():
  resources = {
    "water": 500,
    "milk": 400,
    "coffee": 200,
  }
  return resources
vague cairn
#

Its like a virtual coffee machine. Im trying to make that the owner of the machine can refill with the amount that he has.

austere flume
#

oh then I assume there's a cost to refill

vague cairn
#

The original idea doesnt have that included, im taking it step by step.

austere flume
#

Then you also need to calculate how much the owner can refill

#

do you still need the cost or how?

#

What's going to prevent the player to refill to the max?

vague cairn
#

I guess nothing

#

Im still new to python so im working on simple projects

austere flume
#

sure np

vague cairn
#
resources_list_new = []
    resources_list = list(resources.values())
    for amount in range(0,3):
        resources_list_new.append([resources_list[amount]])
    #resources_list_new = [int(n) for n in resources_list_new]
    print(type(resources_list_new[0]))
    print(resources_list_new)


    if user_order == "refill":
        refill_resources = (input("Input the amount to refill in this order 'water','milk','coffee' or type 'cancel' to cancel refill: "))
        # Capacity: water = 500ml, milk = 400ml, coffee = 300 gram
        refill_amount_list = [refill_resources]
        refill_amount_list = refill_resources.split(' ')
        length_refill = len(refill_amount_list)
        refill_amount_list = [int(n) for n in refill_amount_list]
        print(type(refill_amount_list[0]))
        for addition in range(0,length_refill  + 1):
            resources["water"] + refill_amount_list[addition]
#

this is the code for the refill

austere flume
austere flume
vague cairn
#

But now i wont need that for loop. Since we already fixed it.

#

Now i just want to loop in the dictionary values to add the number that the user inputs.

#

Nevermind i made it much simpler

#

Thanks for the help! Much appreciated!

ashen slateBOT
#
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.