Python Forum
Function not returning correct value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function not returning correct value
#1
I have been making a question/answer code with the function that follows but have fallen into trouble using the return statement.
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")
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
Reply


Messages In This Thread
Function not returning correct value - by TsG009 - Nov-23-2017, 07:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can anyone spot why this function is returning an empty list? hhydration 2 1,955 Nov-18-2020, 06:16 AM
Last Post: deanhystad
  Why is this function returning none? wlsa 2 5,940 Sep-30-2018, 01:36 PM
Last Post: gruntfutuk
  Function for returning absolute numbers Ernstblack 2 3,360 Oct-02-2017, 05:48 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020