#๐ Project still in work
75 messages ยท Page 1 of 1 (latest)
@past bone
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.
!code
please wrap your code in a code block
it'll be easier to read
how do i do that
follow this
i tried and it looks better when i edit but when i hit save it go back to text
here a image click for quality
print("hi everyone")
def protein_sources():
egg = "6g protein {155kcal}"
chicken = 21 , "g protein" , "{239kcal}"
peanut = "26g protein {588kcal}"
salmon = "20g protein {208kcal}"
pasta = "5g protein {157kcal}"
print(chicken)
protein_sources()
def calculations(chicken):
food = int(input("what did you eat : " ))
grams = int(input("how many grams : " ))
if food == "chicken":
calc = chicken/100 * grams
print(calc)
calculations(chicken)
where are you trying to pull the last line's chicken from?
from the top
how can i do that master
if you didn't access the function, it will remain in the function and won't be defined yet
in python, variables have scopes
chicken (inside the protein_sources function) is inside that function's scope
once the function finishes, the variable stops existing
move your variable definitions outside the function, since you don't edit them anyway
your code should look something liek this
print("hi everyone")
egg = "6g protein {155kcal}"
chicken = 21 , "g protein" , "{239kcal}"
peanut = "26g protein {588kcal}"
salmon = "20g protein {208kcal}"
pasta = "5g protein {157kcal}"
def protein_sources():
print(chicken)
protein_sources()
def calculations(chicken):
food = int(input("what did you eat : " ))
grams = int(input("how many grams : " ))
if food == "chicken":
calc = chicken/100 * grams
print(calc)
calculations(chicken)
I would really be using a dictionary for that data though
it aint meant to be dictonairy i just like decoration
you're just making it harder for yourself though
why i like decorations
making multiple variables like this can get messy fast
imagine if you needed to add, say, 10 more protein sources
i always put symbols underscores and other stuff to decorate my code when done
or add a protein source while the program is running (i.e. user prompt)
you could do it but it'd be easier to do so with a dict
what is more effective way master
use a dict
with a dictionary, as we suggested
I don't understand why you'd ignore our suggestions after asking for help
how i do that it 2 years since i use a dictonary
you dont have to do all my code just show example
print("hi everyone")
protein_sources = {
"egg": "6g protein {155kcal}",
"chicken": "21g protein {239kcal}",
# and so on...
}
# Removed the protein_sources function since it does nothing but print chicken
def calculations(chicken):
food = int(input("what did you eat : " ))
grams = int(input("how many grams : " ))
if food == "chicken":
calc = chicken/100 * grams
print(calc)
calculations(protein_sources["chicken"])
will it work for the math tho
Even still, I would separate the data so it's not just a string as the value
protein_sources = {
"egg": (6, 155),
"chicken": (21, 239),
# and so on...
}
I would do it like this, where the value data is the protein in grams and kcal
now you can easily look up a protein and get the relevant data
that is looked up but will that work with the math still
if the numbers are the same, why wouldn't it?
you're going to have to change your logic a bit
but the math is the same
ok ill try that
this prevents you from have to write an if for every single protein
as mentioned above, imagine you had 50 protein sources. Would you want to write 50 if statements?
you gotta do what you gotta do
im willing to do allat to track my protein with accuracy
but i wll try dictonary
!e
protein_sources = {
"egg": (6, 155),
"chicken": (21, 239),
# and so on...
}
protein = 'chicken'
if protein in protein_sources:
info = protein_sources[protein]
print(f'{protein} has {info[0]}g of protein and {info[1]} kcal')
@split olive :white_check_mark: Your 3.12 eval job has completed with return code 0.
chicken has 21g of protein and 239 kcal
work smarter, not harder
or show how much extra ur willing to sacrafice for something because if you dont go far for something then you dont really want or need it
Why even ask us for help then?
what is the info{0}
bcs i want to learn
info = protein_sources[protein]
ohhh
I'm using info as the variable to look up the value of that associated protein
info[0] is the grams, and info[1] is the kcal
for oposite i call for -1 then 0 right
just do [1] and [0]
ok thanks i lofe you
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.
๐ Project still in work