0

I'm taking this programming class and one of our assignments is to make a quiz and return right and wrong answers. So I've coded it all and the only thing that seems to be wrong is my passing of arguments. My result is always zero I'd appreciate any help given.

Here is my code:

correctAnswers = 0
wrongAnswers = 0

answer = input("Question 1: 5-3")
if(answer == 2):
    correctAnswers + 1
else:
    wrongAnswers +1

grade(correctAnswers, wrongAnswers)

grade(right, wrong):
    right - wrong
    print("You have a total of " + str(right) + " correct answers")

It's just a snippet of it, but gets the idea across.

7
  • This code never assigns a value back to correctAnswers or wrongAnswers nor stores the value that right - wrong evaluates to. Commented Feb 14, 2015 at 0:14
  • What is right - wrong supposed to do? You're doing subtraction, but not saving the result anywhere. Commented Feb 14, 2015 at 0:14
  • It seems like you were asleep on the day the class studied assigning variables. Commented Feb 14, 2015 at 0:15
  • 1
    Then you need to provide a complete example that reproduces the exact problem you're facing. stackoverflow.com/help/mcve Commented Feb 14, 2015 at 0:26
  • 1
    a minimal example is never too long... Commented Feb 14, 2015 at 0:36

1 Answer 1

2

You are comparing a string to and int:

  int(input("Question 1: 5-3")) # cast to int

You also need += not just + correctAnswers += 1

You may also have meant to print(right - wrong)?

Instead of casting you can also compare to a string:if answer == "2"

"foo"+ "bar" will indeed create a new object but that is concatenation, but when you want to increment a variable you need to use += which is short form for var = var + 1.

lastly when casting a string to an int, to make sure the user enters valid data using a while loop which will keep asking until the user enters data that can be cast to an int and using a try/except to catch bad input would be a good idea:

while True:
    try:
        answer = int(input("Question 1: 5-3"))
        break
    except ValueError:
        print("Invalid input")
if answer == 2:
    correctAnswers += 1
else:
    wrongAnswers += 1
Sign up to request clarification or add additional context in comments.

5 Comments

@Christopherm have you used += also?
I'm going to do that now. Thank you
It worked! Would you mind explaining how that makes it work compared to just the + rather than +=?
@Christopher, added an example to bottom of my answer
@Christopher, you need to assign your value somewhere. correctAnswers += 1 is shorthand for: correctAnswers = correctAnswers + 1. It is not uncommon to misunderstand the idea of assigning values. correctAnswers + 1 does return a value, but that value needs to get assigned somewhere, otherwise it will not be used. Open a python terminal and type 1 + 1 what do you get? 2, but that value is not assigned to a variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.