0

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.

  • You need to append `wrong` to the other two strings on each side of it. Using + or ,. E.g. `print("Hello" + "World") ` – Luke Joshua Park Oct 13 '15 at 22:25
  • Related (not exactly a dupe though): http://stackoverflow.com/questions/13945749/string-formatting-in-python-3 – Daniel Pryden Oct 13 '15 at 22:25
  • Are you using Python 2 or 3? – TigerhawkT3 Oct 13 '15 at 22:28
  • 1
    @TigerhawkT3 Because `print ("You will get a 100%")` and `input()` function , I think OP is using Python 3. – Remi Guan Oct 13 '15 at 22:32
  • What if score is equal to passscore? Plus, how do you feel wrong is a percentage? – Bill Woodger Oct 13 '15 at 22:34
  • 1
    @KevinGuan - Technically, you can use parentheses in a Python 2 `print` statement. Something like `print ('hi')` produces identical results in Python 2 and 3, but `print ('hi', 'there')` produces the tuple `('hi', 'there')` in Python 2 and the string `'hi there'` in Python 3. Also, `input()` exists in Python 2, and will evaluate the entered string into an `int` or `float` or whatever - it's Python 3 that would need to cast the string. – TigerhawkT3 Oct 13 '15 at 22:41
  • @TigerhawkT3 However usually we use `print 'foobar'` in Python 2 :) And about `print ('hi')` can works on Python 2 because it will create a tuple that only one element in it so the `()` willn't be print out? – Remi Guan Oct 13 '15 at 22:45
  • 1
    @KevinGuan - Nope, that's not a `tuple`. The parentheses only affect grouping. `1,` is a `tuple`, while `(1)` is an `int`. – TigerhawkT3 Oct 13 '15 at 23:10
  • @TigerhawkT3 Got it, thanks :) – Remi Guan Oct 13 '15 at 23:11

6 Answers6

3

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.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
2

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))
C. Murtaugh
  • 574
  • 4
  • 15
1

You can put multiple arguments by comma separating them..

print "You can get", wrong, "% back  with text corrections"
ergonaut
  • 6,929
  • 1
  • 17
  • 47
1

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.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
1

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")
digitaLink
  • 458
  • 3
  • 17
1
  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)) 
Remi Guan
  • 21,506
  • 17
  • 64
  • 87