8 Replies - 871 Views - Last Post: 19 April 2011 - 09:51 AM Rate Topic: -----

#1 chadillac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-March 11

Have variable values in code

Posted 17 April 2011 - 12:12 PM

I am suppose to "Our original game awards one point for every correct answer. Now we will add to the definition of each question a number that is the number of points that will be awarded when a user correctly answers the question.



1. To add this functionality, you will need to:

a. Create a new file trivia_points.txt. The format of this file will be the same as that for trivia.txt except that you need to add a number (the point value) to each question. You may do this any way you like, but probably the easiest is to put it on its own line, probably right after the explanation line.

b. Modify your code so that:

i. It reads the file trivia_points.txt rather than trivia.txt.

ii. Each time it reads a question it reads in the point value and stores it (in a variable). Important: exactly how you do this depends on the decision you made above in part a. Also, remember: when you read in a number, it is read as a string. You will need to be able to use it as a number (so you can add it to the score). The following code converts a string in the variable y to a number:
y = int(y)

iii. Each time a question is answered correctly, the score should be incremented by the given point value.

I don't really understand B.ii very well and how to get the code to read the point values. Help on that would be appreciated.

Here is the code to the main program the rest is all in a .txt file the program reads.

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

    # get first block
    category, question, answers, correct, explanation = 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 += 1
        else:
            print("\nWrong.", end=" ")
        print(explanation)
        print("Score:", score, "\n\n")

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

    trivia_file.close()

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


Is This A Good Question/Topic? 0
  • +

Replies To: Have variable values in code

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Have variable values in code

Posted 17 April 2011 - 07:51 PM

You would simply have to modify your getBlock() method to return an additional variable called values, which stores the values of the point (at least...that's what I can tell). Then, if you get the answer right, you get a certain amount of extra points. Try this method and see.
Was This Post Helpful? 0
  • +
  • -

#3 chadillac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-March 11

Re: Have variable values in code

Posted 18 April 2011 - 09:45 AM

So I should add the line at line 28? And how would i make it keep track of the points the person already has?
Was This Post Helpful? 0
  • +
  • -

#4 chadillac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-March 11

Re: Have variable values in code

Posted 18 April 2011 - 05:46 PM

I am supposed to "
ii. Each time it reads a question it reads in the point value and stores it (in a variable). Important: exactly how you do this depends on the decision you made above in part a. Also, remember: when you read in a number, it is read as a string. You will need to be able to use it as a number (so you can add it to the score). The following code converts a string in the variable y to a number:
y = int(y) "
 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) 

    return category, question, answers, correct, explanation

def welcome(title):
    """Welcome the player and get his/her name."""
    print("\t\tWelcome to Trivia Challenge!\n")
    print("\t\t", title, "\n")
 
def main():
    trivia_file = open_file("trivia.txt", "r")
    title = next_line(trivia_file)
    welcome(title)
    score = 0

    # get first block
    category, question, answers, correct, explanation = 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 += 1
        else:
            print("\nWrong.", end=" ")
        print(explanation)
        print("Score:", score, "\n\n")

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

    trivia_file.close()

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


So far I think it needs to go somewhere under the "next block" program and the "get next block" in the main file.
I don't really know what I need to write for the code or how to make it add. Any help would be appreciated.

I also already edited the .txt file with an extra line on each question with the variable number.
Was This Post Helpful? 0
  • +
  • -

#5 chadillac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-March 11

Re: Have variable values in code

Posted 18 April 2011 - 10:07 PM

Ok this is the code I have so far
 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) 

    return category, question, answers, correct, explanation

def welcome(title):
    """Welcome the player and get his/her name."""
    print("\t\tWelcome to Trivia Challenge!\n")
    print("\t\t", title, "\n")
 
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 = 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 += 1
        else:
            print("\nWrong.", end=" ")
        print(explanation)
        print("Score:", score, "\n\n")

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

    trivia_file.close()

    print("That was the last question!")
    print("You're final score is", score)
 
main()  
input("\n\nPress the enter key to exit.")

I was thinking I need to add the points variable to both the next block function after the explanation part. and in the main function too. I don't know how to make the program read it the variables and add them yet though
Was This Post Helpful? 0
  • +
  • -

#6 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 60
  • View blog
  • Posts: 685
  • Joined: 19-January 09

Re: Have variable values in code

Posted 18 April 2011 - 11:16 PM

I am really confused to what you are asking. Would you be able to tell us any errors you are getting or what isn't working?

This post has been edited by Eric115: 18 April 2011 - 11:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5669
  • View blog
  • Posts: 22,516
  • Joined: 23-August 08

Re: Have variable values in code

Posted 19 April 2011 - 06:19 AM

Merged duplicate topics. Please do not create a new topic on pretty much the same subject when you already have one.
Was This Post Helpful? 0
  • +
  • -

#8 chadillac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-March 11

Re: Have variable values in code

Posted 19 April 2011 - 08:32 AM

I added a line in the txt file that has a number in it, that number is the value of points that the question is worth. I don't know how to change the code I have to make it where the code reads and adds the number in the extra block I added to the txt file. I tried adding a "points variable" after the variables on line 36 and then again in the main file but I don't know how to make it work.
Was This Post Helpful? 0
  • +
  • -

#9 chadillac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-March 11

Re: Have variable values in code

Posted 19 April 2011 - 09:51 AM

Ok, I got the variable to work but it won't add with out crashing the program. It can read and print (points) when its seeing if the problem was answered right, but when i tried to add (points) when it adds the score it crashes.
# 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)

    points = next_line(the_file)

    return category, question, answers, correct, explanation, points

def welcome(title):
    """Welcome the player and get his/her name."""
    print("\t\tWelcome to Trivia Challenge!\n")
    print("\t\t", title, "\n")
 
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, points = 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 += 1 + (points)
            print(points)
        else:
            print("\nWrong.", end=" ")
        print(explanation)
        print("Score:", score, "\n\n")

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

    trivia_file.close()

    print("That was the last question!")
    print("You're final score is", score)
 
main()  
input("\n\nPress the enter key to exit.")



I don't know how to add the points variable part with out it crashing. Help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1