2 Replies - 2187 Views - Last Post: 19 April 2011 - 05:40 AM Rate Topic: -----

#1 recursive83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-February 11

Using variable from another function

Posted 19 April 2011 - 12:43 AM

Hey there, this is a very fundamental concept and I've looked it up and global variables are not working for me. I need to use the variable score which is in the function main in another function called judgment, so based on the score of the trivia, I can tell the user how they did (which is why i called the judgment function at the very bottom of main). I have pasted the code here, and also attached it to the document. It gives error that name score is not defined in judgment function

# Trivia Challenge
# Trivia game that reads a plain text file

import sys

def open_file(file_name, mode):
    """Open a file."""
    try:
        the_file = open(file_name, mode)
    except IOError as e:
        print("Unable to open the file", file_name, "Ending program.\n", e)
        input("\n\nPress the enter key to exit.")
        sys.exit()
    else:
        return the_file

def next_line(the_file):
    """Return next line from the trivia file, formatted."""
    line = the_file.readline()
    line = line.replace("/", "\n")
    return line

def next_block(the_file):
    """Return the next block of data from the trivia file."""
    category = next_line(the_file)
    
    question = next_line(the_file)
    
    answers = []
    for i in range(4):
        answers.append(next_line(the_file))
        
    correct = next_line(the_file)
    if correct:
        correct = correct[0]
        
    explanation = next_line(the_file)

    point = next_line(the_file)
    

    return category, question, answers, correct, explanation, point

def welcome(title):
    """Welcome the player and get his/her name."""
    print("\t\tWelcome to Trivia Challenge!\n")
    print("\t\t", title, "\n")
 
def judgement(score):
        
    if  score > 0 and score <= 5:
            print("You can do better.")
    elif score >= 6 and score <= 10: 
            print("You did okay.")
    elif score >= 11 and score <= 14:
            print("You did average.")
    elif score >= 15 and score <= 19:
            print("You did above average.")
    elif score >= 20 and score <= 24:
            print("You did excellent.")
    else:
        print("Does not exist.")

def main():
    trivia_file = open_file("trivia_points.txt", "r")
    title = next_line(trivia_file)
    welcome(title)
    score = 0

    # get first block
    category, question, answers, correct, explanation, point = next_block(trivia_file)
    while category:
        # ask a question
        print(category)
        print(question)
        for i in range(4):
            print("\t", i + 1, "-", answers[i])

        # get answer
        answer = input("What's your answer?: ")

        # check answer
        if answer == correct:
            print("\nRight!", end=" ")
            score += int(point)
        else:
            print("\nWrong.", end=" ")
        print(explanation)
        print("Score:", score, "\n\n")

        # get next block
        category, question, answers, correct, explanation, point = next_block(trivia_file)

    trivia_file.close()

    print("That was the last question!")
    print("You're final score is", score)

judgement(score)
 
main()  
input("\n\nPress the enter key to exit.")



Thanks for the help. Appreciate it

Attached File(s)


This post has been edited by recursive83: 19 April 2011 - 12:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using variable from another function

#2 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Using variable from another function

Posted 19 April 2011 - 01:46 AM

Looks like you want to call judgement(score) from inside main(). But then it should be indented accordingly.
Indent line 99 so that judgement(score) is part of the main() function.
Was This Post Helpful? 0
  • +
  • -

#3 recursive83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-February 11

Re: Using variable from another function

Posted 19 April 2011 - 05:40 AM

View PostNallo, on 19 April 2011 - 01:46 AM, said:

Looks like you want to call judgement(score) from inside main(). But then it should be indented accordingly.
Indent line 99 so that judgement(score) is part of the main() function.


thanks for the input. it did work just fine. I've got more to do on this one and I'll get back on if I get stuck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1