#π Cant seem how to do this.
91 messages Β· Page 1 of 1 (latest)
@vague cairn
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.
you use the .values() instead of .items()
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)
You can't split a list.
its already splitted
Im trying to split the strings in the list to convert them into an integer.
resources_list_new is already a list
you're saying you want to split each element in the list further?
Basically im just trying to convert the numbers inside the list into integers
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?
You're already doing it here
resources_list_new = [int(n) for n in resources_list_new]
I'm saying you don't need to split it again before that line
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'
You'll need to print out resources_list_new. It is a list but as it loops over the elements, it found another "list" and you can't int() a list
Yeah the probelm is the data is stored like this for some reason. [[300], [200], [100]]
How can i remove the extra brackets ?
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?
Im trying to add it with a user input
Like if the user input is 10, you would 300 + 10?
yeah
Can you show me a sample of your resources dictionary?
The way you're doing this is very... farfetched
resources = {
"water": 300,
"milk": 200,
"coffee": 100,
}
ok that makes sense
So if you wanna update a value in your resources by 10, you just add to it by its key
yeah ive tried it before but it didnt work
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"])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
300
oh
So that returns the value
So if you wanna "update" that value, you reassign it with the same key
so what about if i wrote it like this print(resources)["water"]
!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
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 300
002 | 310
hmm
But the resources get updated when a user places the order, so its not always 300, 200,100 etc.
!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
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 300
002 | 310
003 | {'water': 310, 'milk': 200, 'coffee': 100}
You can always manipulate the values whenever but it stores them consistently in the resources dictionary
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
!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
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{'water': 299, 'milk': 199, 'coffee': 99}
you want to refill all of the resources?
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: "))
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
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"
Is there a limit to how much resources you want to refill up to?
Can they refill up to 99999999999 per resource?
yes 500ml , 400m, 200 grams
400m?
ml*
ok
Why not just reset the values to their max?
def refill():
resources = {
"water": 500,
"milk": 400,
"coffee": 200,
}
return resources
Its like a virtual coffee machine. Im trying to make that the owner of the machine can refill with the amount that he has.
oh then I assume there's a cost to refill
The original idea doesnt have that included, im taking it step by step.
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?
sure np
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
This line is unnecessary btw
refill_amount_list = [refill_resources]
Since you're already reassigning the split values in the next line
I think you're meant to += instead of + in your for loop
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!
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.