#parse function

53 messages · Page 1 of 1 (latest)

quartz gulch
#

i need to write a function that takes an input, say "'1 large banana" and then modifys it to output (1.0, 'large', 'banana')
this is what i have written so far but it seems to be incorrect

def parse_ingredient(raw_ingredient_detail: str):
raw_ingredient_detail.split()
parse = raw_ingredient_detail.split()
return(parse[float, (0), (1)])

the goal is for it to look like this
parse ingredient(raw ingredient detail: str)
outputs: tuple[float, str, str]

#

my friend said he used an if loop but i cant think of how that would be implemented

jolly sinew
#

Are you allowed regex?

quartz gulch
#

im not sure what that is. its just for a first semester coding project so i wouldn't imagine anything more advanced than super basic?

#

im not allowed import statements tho, not sure if that is relevant

jolly sinew
#

I see

#

Try returning just parse

#

See what you get and compare it to the expected output

#

You'll see that it's pretty much there after converting the first element into a float

quartz gulch
#

like this you mean?

#

the last line is return(parse) sorry its a bit cut off

quartz gulch
jolly sinew
quartz gulch
#

i cant run it and ive asked around and im not sure why. all i have been told by other people doing this assignment is that the function just needs to be acceptable to the autograder.

for example, the assignment asks for a function like this:

get recipe name(recipe: tuple[str, str]) -> str
Returns the name of the recipe
.
the correct answer to this was

def get_recipe_name(recipe: tuple[str, str]):
return(recipe[0])

jolly sinew
#

!exec ```py
def parse_ingredient(raw_ingredient_detail: str):
parse = raw_ingredient_detail.split()
return parse
print(parse_ingredient("1 large banana"))

final roostBOT
jolly sinew
#

@quartz gulch

#

Doesn't this look close to what you want?

quartz gulch
#

yes?

#

the issue is that it needs to be applicable to everything and not just "1 large banana"

#

im having difficulty putting it into words but the function basically needs to be able to filter anything that is put through it and output something like ['1', 'large', 'banana']
there are other ingredients such as coffee granules that need to be filtered as 'coffee granules' and not 'coffee', 'granules'

#

each ingredient is in the same format tho, its always amount (1, 0.25, 20 etc), measurement (large, cup, handful etc), and then ingredient (peanut butter, coffee granules, banana etc)

#

heres the provided example if it helps at all

#

Example:

parse_ingredient('0.5 tsp coffee granules')
(0.5, 'tsp', 'coffee granules')

parse_ingredient('1 large banana')
(1.0, 'large', 'banana')

#

someone on another forum is telling me to use the sep argument

jolly sinew
quartz gulch
#

maxsplit Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"

im going to keep reading but i gather i can use this to make the split function not split up my two word ingredients and keep them whole by telling it to only split two whitespaces instead of three

#

thank you for all the help so far

jolly sinew
#

Yeah

#

Maybe try making maxsplit 2

quartz gulch
#

i changed the code to this

def parse_ingredient(raw_ingredient_detail: str):
raw_ingredient_detail.split(" ", 2)
parse = raw_ingredient_detail.split(" ", 2)
return(parse)

#

based of the pattern i gathered in the website you sent

#

it failed but i think that is because i havent converted the number to a float which is the next step i am guessing

#

def parse_ingredient(raw_ingredient_detail: str):
raw_ingredient_detail.split(float[0], " ", 2)
parse = raw_ingredient_detail.split(float[0], " ", 2)
return(parse)
this failed. not sure how to make the number into a float

jolly sinew
#

Do you know to change the value of an element in a list?

quartz gulch
#

not 100% no

#

actually just no

jolly sinew
#

What you do is you index the list and give it a new value

#

For example

#

!exec ```py
example = [1, 2, 6]
print(example)
example[0] = "lol"
print(example)

final roostBOT
quartz gulch
#

how do i apply this to changing the number tho? currently it is a string and needs to be a float

#

im having trouble picking up what you're putting down, sorry

jolly sinew
#

Well all you have to do is make the number into a float on the right side of the equals sign

quartz gulch
#

but how do i do that if i dont have an equals sign in my code?

#

i have the splitting part done with the maxsplit, all it needs to do now is convert the first element of the split into a float and leave the other two elements as strings

quartz gulch
#

possibly not to the degree that you want
i think that you make 'example' gets the tuple [1, 2, 6]
then you go into 'example' and you tell the [0] element, which in this case is '1' and you tell it to = 'lol'
then you print the updated version

#

right?

jolly sinew
#

Yup

#

That's pretty much how you change the value of a list element

quartz gulch