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!

  • 2
    What version of Python are you using? – DavidG May 15 '20 at 11:16
  • I don't get any error! What syntax error you get? – Ala Tarighati May 15 '20 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. – Prithvi Raj May 15 '20 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. – Gino Mempin May 15 '20 at 11:22
  • Maybe this answers your question? [f-strings giving SyntaxError?](https://stackoverflow.com/q/50401632/2745495) – Gino Mempin May 15 '20 at 11:25

2 Answers2

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/

Kamai
  • 9
  • 2
  • 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. >>> – buttermilkjesus May 15 '20 at 14:05
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.

Krab
  • 2,118
  • 12
  • 23