0

My code is supposed to loop through a list of simple equations (e.g. 1+1 = 2), split the equations from the answers, and then ask the user the equation. I'm getting a SyntaxError on the line where it prints the equation, and I think it has to do with the combination of the variable and the tring since it runs fine when there is no variable inserted.

for question in questions:       #iterate through each of the questions
equation = question.split('=')   #split the equations into a question and an answer
temp = str(equation[0])
print(f'{equation[0]}=')
answer = input()  #Prompt user for answer to equation
if answer == int(equation[1]):   #Compare user answer to real answer
    score +=1                    #If answer correct, increase score

What am I missing here?

clarifications: SORRY! made a mistake and copied my test code. The important line: print(f'{equation[0]}=') has been corrected.

Indentation is identical to what is posted. equation list looks like:

1+1=2
2+2=4
44-15=29
1x2=2

full traceback:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:/Users/nsuess/PycharmProjects/Quiz/exercise.py", line 28
    answer = input(f'{equation[0]}=')  #Prompt user for answer to equation
                                   ^
SyntaxError: invalid syntax

Update: I found that if I change the line to: response = input(equation[0]) then it runs fine, but I need to add an '=' to the end according to the problem statement. When I use print, it puts it on a new line. Is there a workaround for this?

Nick Suess
  • 25
  • 4
  • Can you give an example of the question list? – andrepz Jul 09 '21 at 17:11
  • 1
    Is your indentation identical to what you have in your program? – Axe319 Jul 09 '21 at 17:11
  • 1
    Welcome to SO! Please take the [tour]. For debugging help, you need to provide a [mre] including complete working code (yours has an indentation error and is missing `questions`), input, expected output, and actual output, i.e. the full error message. You can [edit]. See [ask] if you want more tips. – wjandrea Jul 09 '21 at 17:11
  • 2
    What does your error traceback say? Aside from the intention, the syntax error in the print line comes from the fstring if using Python3.5 or lower – Wondercricket Jul 09 '21 at 17:13
  • Indentation is identical to what is posted. equation list looks like: 1+1=2 2+2=4 44-15=29 1x2=2 can't tell about formatting, but it's one equation per line – Nick Suess Jul 09 '21 at 17:13
  • 1
    @NickSuess you should edit your question to add this info instead of posting it into comments – andrepz Jul 09 '21 at 17:14
  • If the error is at `print(f'{temp}')`, maybe the problem is that the version of Python you're using doesn't support f-strings. Either way, that f-string is redundant: use `print(temp)` instead. – wjandrea Jul 09 '21 at 17:14
  • 1
    This is a duplicate of [f-strings giving SyntaxError?](/q/45621722/4518341) – wjandrea Jul 09 '21 at 17:20

2 Answers2

1

Assuming your question list is similar to the following:

questions = ['2 + 2 = 4', '3 - 1 = 2', '2 * 4 = 8']  

This is how I would proceed:

score = 0
for question in questions:
    temp = question.split('=')
    equation = temp[0].strip()
    answer = temp[1].strip()
    resp = input(equation)
    if resp == answer:
        score += 1
print(score)        
itprorh66
  • 3,110
  • 4
  • 9
  • 21
  • This runs when I put it in my code. I think it may have something to do with the .strip, because other than that it looks almost identical to me. Thanks! – Nick Suess Jul 09 '21 at 17:28
  • Oh I just noticed that you have: resp = input(equation) and I have response = input(f'{equation[0]}=') So your code runs because you're not mixing a variable and a string into the input... Maybe I'll try printing the variable first – Nick Suess Jul 09 '21 at 17:30
0

Solved: My IDE was using python 2.7 instead of 3.6 which was needed to print a string and a variable in the same line.

Nick Suess
  • 25
  • 4