0

Learning from the book "python crash course second edition". I'm getting syntaxerrors for the code that is being taught inside the book and don't understand why.

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."
print(bicycles[0].title())
print(message)

Any reasons why? Is the book incorrect?

I'm using sublime text on a MacBook Pro. Thanks!

5
  • 2
    What version of Python are you using? Commented May 15, 2020 at 11:16
  • I don't get any error! What syntax error you get? Commented May 15, 2020 at 11:16
  • f-strings are introduced in python 3, so using python 2 will throw error, can you share the version of python and the exact error. Commented May 15, 2020 at 11:18
  • When asking questions about errors, please provide the actual, complete error message (the one that starts with Traceback). Also, always specify your Python version as different versions could lead to different errors. Commented May 15, 2020 at 11:22
  • Maybe this answers your question? f-strings giving SyntaxError? Commented May 15, 2020 at 11:25

2 Answers 2

1

You might be using python version 3.6 and below, message = f"My first bicycle was a {bicycles[0].title()}." 'f' strings are introduced in python 3.6 and above. So check you current python version, if your version below 3.6 then surely that's the error's route cause. Learn more about python 'f' string visit https://www.python.org/dev/peps/pep-0498/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the comment. I typed 'python' into my terminal and got this response: Last login: Thu May 14 20:29:30 on ttys000 Seans-MBP:~ seanfairless$ python Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
0

The code runs OK for me.

The syntax with f (f"My first bicycle was a {bicycles[0].title()}.") is new from Python 3.6.

Check that your Python version is recent enough. It's also useful to post the exact error you get.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.