Nov-23-2017, 07:56 PM
I have been making a question/answer code with the function that follows but have fallen into trouble using the return statement.
The problem I have faced involves the return statement not working so that the global variable for the score is not being increased, if the user correctly answers. I've implemented this line in other parts of the code: such as after increasing the score by one but haven't got anywhere with this. In the main code I have been using file handling instead of printing each question individually. I tried using while a loop to loop through the file however this proved difficult as the code printed every other item in the file and came up with an error. If you have any idea why this is happening for ether scenario that would be appriciated. Python v3.6.3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
s = 0 d = input ( "Enter difficulty (e/m/h)" ) def question(d, score): print ( "1 + 1" ) if d = = 'e' : print ( "2 1" ) elif d = = 'm' : print ( "2 1 0" ) else : print ( "2 1 0 -1" ) answer = input ( "Enter the correct answer!" ) if answer = = '2' : print ( "Correct" ) score + = 1 print (score, "Score in code." ) else : print ( "Incorrect the answer was 2." ) return s #This line of code does not appear to be running. print ( "question 1" ) question(d, s) print (s, "Score" ) |