-2

I am using Spyder with Python 3.9.12

Here is the code I have inside Spyder:

user_input = (input('Please enter a number between 1 and 12:>>' ))

while (not user_input.isdigit()) or (int(user_input) < 1 or int(user_input) > 12):
    print('Must be an integer between 1 and 12')
    user_input = input('Please make a selection:>> ')
user_input = int(user_input)
print('============================')
print()
print(f"This is the "{user_input}" times table")
print()
for i in range(1,13):
    print(f""{i}" x "{user_input}" = "{i=user_input}"")

Error output from Spyder:

runfile('/Users/user/spyder-files/For-Loops.py', wdir='/Users/user/spyder-files')
  File "<unknown>", line 49
    print(f""This is the "{user_input}" times table"")
             ^
SyntaxError: invalid syntax

I tried using single quotes but get the same error message:

user_input = (input('Please enter a number between 1 and 12:>>' ))

while (not user_input.isdigit()) or (int(user_input) < 1 or int(user_input) > 12):
    print('Must be an integer between 1 and 12')
    user_input = input('Please make a selection:>> ')
user_input = int(user_input)
print('============================')
print()
print(f'This is the '{user_input}' times table')
print()
for i in range(1,13):
    print(f''{i}' x '{user_input}' = '{i=user_input}'')

Same error:

runfile('/Users/user/spyder-files/For-Loops.py', wdir='/Users/user/spyder-files')
  File "<unknown>", line 49
    print(f'This is the '{user_input}' times table')
                         ^
SyntaxError: invalid syntax

I appreciate any suggestions.

Thanks.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Justin Henson
  • 43
  • 1
  • 9
  • You are allowed to mix single and double quotes to get what you want. Did you mean: `print(f'This is the "{user_input}" times table')`? – quamrana Nov 19 '22 at 17:13
  • I changed the code to single quotes as you suggested, but now have a different error message: **runfile('/Users/user/spyder-files/For-Loops.py', wdir='/Users/user/spyder-files') File "", line 52 print(f'"{i}" x "{user_input}" = "{i=user_input}"') ^ SyntaxError: f-string: expecting '}'** – Justin Henson Nov 19 '22 at 17:21
  • Yes, that's right - I found that one as well. Please be patient whilst Matthias fixes their answer. – quamrana Nov 19 '22 at 17:23
  • Updated code: user_input = (input('Please enter a number between 1 and 12:>>' )) while (not user_input.isdigit()) or (int(user_input) < 1 or int(user_input) > 12): print('Must be an integer between 1 and 12') user_input = input('Please make a selection:>> ') user_input = int(user_input) print('============================') print() print(f'This is the "{user_input}" times table') print() for i in range(1,13): print(f'"{i}" x "{user_input}" = "{i=user_input}"') – Justin Henson Nov 19 '22 at 17:23

1 Answers1

1

You used double quotes in f""{i}" x "{user_input}" = "{i=user_input}"". Now the string starts at the first double quote and ends at the second. The following text now leads to a SyntaxError.

You could use triple quotes to define the string. The fourth is now part of the strings content.

f""""{i}" x "{user_input}" = "{i*user_input}""""

Or use different quotes

f'"{i}" x "{user_input}" = "{i=user_input}"'
Matthias
  • 12,873
  • 6
  • 42
  • 48