Python-mode: Pyflakes Invalid code Error for simple code

Created on 28 Jul 2020  路  5Comments  路  Source: python-mode/python-mode

Hi everyone,

I am new to python and I'm having a problem with a simple recipe adjuster code in my book.

I already tried to solve it on my own and even looked at similar codes but still can't resolve the problem. (Error is on line 8)

Any explanation you can give me regarding this issue?

Thank you and I appreciate all the help!

A cookie recipe calls for the following ingredients: 1.5 # cups of sugar 1 cup of butter 2.75 cups of flour The recipe produces 48 cookies with this amount of the ingredients. Write a program that asks the user how many cookies he or she wants to make, then displays the number of cups of each ingredient needed for the specified number of cookies.

````
recipe_sugar = 1.5
recipe_butter = 1
recipe_flour = 2.75
recipe_cookies = 46

num_cookies = (float(input(' How many cookies do you want to make? '))

new_sugar = (num_cookies / recipe_cookies ) * recipe_sugar
new_butter = ( num_cookies / recipe_cookies) * recipe_butter
new_flour = ( num_cookies / recipe_cookies) * recipe_flour

print('The new amount of ingredients you need is:')

print('sugar = ' , new_sugar)
print('butter = ', new_butter)
print('flour = ', `new_flour)
````

All 5 comments

Hi @Punchline808,

first of all, can you please put your code in a fenced code block ? That will help understanding what exactly is part of your code and what is not.

Second, which line exactly is presenting error, and which error is being "raised"?

Hi @Punchline808,

first of all, can you please put your code in a fenced code block ? That will help understanding what exactly is part of your code and what is not.

Second, which line exactly is presenting error, and which error is being "raised"?

Thank you @diraol,
I just put my code in the code block sorry for that. I'm getting a Pyflakes Error Invalid Syntax for line
new_sugar = (num_cookies / recipe_cookies ) * recipe_sugar

Hi @Punchline808, you have an open parenthesis on L6 where you prompt the user for num_cookies value. This causes Python to think that the beginning of L8 where you assign new_sugar variable is part of the input to the parentheses you started on L6, immediately before the float cast, so Python sees a syntax error on L8.

If you add a closing parenthesis to L6, your particular error will be resolved. Alternatively, you can remove the opening parenthesis before the float cast, closing that entire assignment value in parentheses isn't necessary.

Your final line also has a stray character before thenew_flour` variable.

Here is a version of your code with these errors removed as well as some spaces removed which minorly break with PEP8 style standards.

recipe_sugar = 1.5
recipe_butter = 1
recipe_flour = 2.75
recipe_cookies = 46

num_cookies = float(input(' How many cookies do you want to make? '))

new_sugar = (num_cookies / recipe_cookies) * recipe_sugar
new_butter = (num_cookies / recipe_cookies) * recipe_butter
new_flour = (num_cookies / recipe_cookies) * recipe_flour

print('The new amount of ingredients you need is:')

print('sugar = ', new_sugar)
print('butter = ', new_butter)
print('flour = ', new_flour)

@jakecolston Omg thank you so much! I can't believe I didn't see that.

Happy to help; it's good to make a habit of checking lines near where Python runs into the syntax errors, in addition to the line itself. And as your scripts/programs get more complex, it can often be that the real cause of errors is further upstream in the program's process than where the final error occurred.

python-mode can be of great assistance in figuring them out as you write :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timfeirg picture timfeirg  路  4Comments

qazip picture qazip  路  5Comments

feoh picture feoh  路  7Comments

randomizedthinking picture randomizedthinking  路  7Comments

eschao picture eschao  路  5Comments