highlandertf2
highlandertf2

Reputation: 51

Invalid syntax with if

this is a pretty simple idea, you enter your test score and if you got above a 70% (35/50) you can do corrections for 1 pt back essentially giving you a 100%. If you get under 70% you can do corrections for 1/2 a point back.

this is giving me a invalid syntax and putting the cursor between the last " and )

score = input("How many problems did you get right on the test?")
maxscore = 50
passscore = 35
wrong = (maxscore - score)

if (score > passscore):
    print ("You will get a 100%")


if (score < passscore):
    print("You can get"(wrong)"% back  with text corrections")

Im terrible at programing so sorry if i seem really stupid here.

Upvotes: 0

Views: 2041

Answers (6)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160677

Before you worry about the syntactic error, you need to transform wrong to an int because input() returns user input as a str type.

score = int(input("How many problems did you get right on the test?"))

If you don't and the user enters a string, the expression:

wrong = (maxscore - score) 

Will raise a TypeError, which essentially means that you cannot subtract a value of type str (score) from a value of type int (maxscore).


As for your syntactic error.

print("You can get"(wrong)"% back  with text corrections")

is syntactically invalid. You need to include wrong as a string by transforming it with str() in your print() call:

print("You can get" + str(wrong) + "% back  with text corrections")

You can see, conversions between different types, depending on the operation can be a mess until you get a hang of them.

Upvotes: 1

C. Murtaugh
C. Murtaugh

Reputation: 663

Everyone has to start somewhere (and I'm still pretty new to Python myself)!

Your first problem is that you need to define score as an int:

score = int(input("How many problems did you get right on the test?"))

Then there are at least two solutions to fix that last line of code. One is to use + to separate your strings of text, plus str to convert wrong to string format:

print("You can get " + str(wrong) + "% back  with text corrections")

Or you can use the .format approach, which is more "Pythonic":

print("You can get {0}% back  with text corrections".format(wrong))

Upvotes: 2

digitaLink
digitaLink

Reputation: 458

If you want a string concatenated you need to add + between variable names and Strings. Replace your second print line with:

print("You can get " + str(wrong) + "% back  with text corrections")

Upvotes: 1

Remi Guan
Remi Guan

Reputation: 22312

  1. First, score must be int. So you need use int() function to do that.
  2. And if don't need (), just remove them.
  3. Then , the problem is about print("You can get"(wrong)"% back with text corrections"), you should use + or , or .format(), etc. here. And remember use str() to convert it to string.

score = int(input("How many problems did you get right on the test?"))
maxscore = 50
passscore = 35
wrong = (maxscore - score)

if score > passscore:
    print("You will get a 100%")


if score < passscore:
    print("You can get "+str(wrong)+"% back with text corrections")

This is the simplest way, but use .format() will be more clear like this:

print("You can get {0}% back  with text corrections".format(wrong)) 

Or like this:

print("You can get {wrong}% back  with text corrections".format(wrong=wrong)) 

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49330

The problem is here:

print("You can get"(wrong)"% back  with text corrections")

This is not the correct way to insert a variable into a string. You have several options:

print("You can get " + str(wrong) + "% back with text corrections")

Or:

print("You can get %d%% back with text corrections" % wrong)

Or:

print("You can get {}% back with text corrections".format(wrong))

Or:

print("You can get ", wrong, "% back with text corrections", sep='')

Also, if you're using Python 3, you'll need to do score = int(input(... to cast the string you received as an integer.

Upvotes: 3

ergonaut
ergonaut

Reputation: 7057

You can put multiple arguments by comma separating them..

print "You can get", wrong, "% back  with text corrections"

Upvotes: 1

Related Questions