#๐Ÿ”’ Project still in work

75 messages ยท Page 1 of 1 (latest)

echo plazaBOT
#

@past bone

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.

gloomy crow
#

!code

echo plazaBOT
#
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.

gloomy crow
#

please wrap your code in a code block
it'll be easier to read

past bone
#

how do i do that

rotund gull
past bone
#

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

rotund gull
#
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) 
gloomy crow
#

where are you trying to pull the last line's chicken from?

past bone
#

from the top

rotund gull
#

chicken was never defined

#

you didn't access the top function

past bone
#

how can i do that master

rotund gull
#

if you didn't access the function, it will remain in the function and won't be defined yet

gloomy crow
# past bone from the top

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

past bone
#

oh

#

but how do i make it stay

gloomy crow
#

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) 
split olive
#

I would really be using a dictionary for that data though

past bone
#

it aint meant to be dictonairy i just like decoration

split olive
#

you're just making it harder for yourself though

past bone
#

why i like decorations

split olive
#

Suit yourself ๐Ÿคท

#

You asked for tips to improve this code

gloomy crow
#

imagine if you needed to add, say, 10 more protein sources

past bone
#

i always put symbols underscores and other stuff to decorate my code when done

gloomy crow
#

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

past bone
split olive
gloomy crow
split olive
#

I don't understand why you'd ignore our suggestions after asking for help

past bone
#

how i do that it 2 years since i use a dictonary

#

you dont have to do all my code just show example

gloomy crow
#
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"]) 
past bone
#

will it work for the math tho

split olive
#

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

past bone
#

that is looked up but will that work with the math still

split olive
#

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

past bone
#

ok ill try that

split olive
#

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?

past bone
#

you gotta do what you gotta do

#

im willing to do allat to track my protein with accuracy

#

but i wll try dictonary

split olive
#

!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')

echo plazaBOT
#

@split olive :white_check_mark: Your 3.12 eval job has completed with return code 0.

chicken has 21g of protein and 239 kcal
split olive
past bone
split olive
#

Why even ask us for help then?

past bone
#

what is the info{0}

past bone
split olive
#
info = protein_sources[protein]
past bone
#

ohhh

split olive
#

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

past bone
#

for oposite i call for -1 then 0 right

split olive
#

just do [1] and [0]

past bone
#

if i want calories first

#

yeah that works to

split olive
#

[1] is always kcal

#

[0] is always g

past bone
#

ok thanks i lofe you

echo plazaBOT
#
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.

#

๐Ÿ”’ Project still in work