0

Bit of a python noob and I'm required to add a regular expression in my code but can not get it to work :/ i have searched the error message on google and tried to figure out whats wrong but no luck so i figured next best thing to do would be to ask directly. Here is the code so far:

# allows us to access a random 'key' in the dictionary
import random
import re

# Contains the question and it's correct answer
my_dict =   {
            "A form of Protectionism that imposes a tax on imports" : "tariff",
            "....is quantity of a good or service that consumers are willing and able to     buy at a given price in a given time period" : "Demand", 
            "....is the quantity of a good or service that a producer is willing and able   to supply onto the market at a given price in a given time period" : "Supply",
            "By using ..... businesses can bring down their average costs by producing on a larger scale" : "Economies of scale",
            "The cost of the next best alternative" : "Opportunity Cost",
            ".... is the transfer of assets from the public (government) sector to the private sector." : "Privatisation"
        }


# welcome message
print("Economics Revision App")
print("=======================")



# the quiz will end when this variable becomes 'False'
playing = True


# While the game is running
while playing == True:

    # set the score to 0
    score = 0

    # gets the number of questions the player wants to answer
    num = int(input("\nHow many questions would you like: "))
    num = re.match("r\d[0-9]{2}$", num())
    if match:
        print ('foo')

    # loop the correct number of times
    for i in range(num):

        # the question is one of the dictionary keys, picked at random
        question = (random.choice( list(my_dict.keys())))
        # the answer is the string mapped to the question key
        answer = my_dict[question]

        # print the question, along with the question number
        print("\nQuestion " + str(i+1) )
        print(question  + "?")

        # get the user's answer attempt
        guess = input("> ")

        # if their guess is the same as the answer
        if guess.lower() == answer.lower():
            # add 1 to the score and print a message
            print("Correct!")
            score += 1
        else:
            print("Incorrect guess again!")

    # after the quiz, print their final score  
    print("\nYour final score was " + str(score))

    # store the user's input...
    again = input("Enter any key to play again, or 'q' to quit.")

    #... and quit if they types 'q'
    if again.lower() == 'q':
        playing = False

The code I'm struggling with in questions

1
  • The next best thing is to look at the stacktrace for the exception to see which line the error occurred on. What do you think num() means in the line num = re.match("r\d[0-9]{2}$", num())? Commented Dec 8, 2013 at 17:11

1 Answer 1

1

num = re.match("r\d[0-9]{2}$", num())

num is simply an interger, so num() is invalid.

Should be something like match = re.match("r\d[0-9]{2}$", str(num))

1) it should be match right?

2) re is working on str, so the argument passed should be str(num)

Then the code should be fine and fun as well. :)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it accpets the integer but now it wont run the loop of the program?
@user3080288 It actually works quite well on my computer. Are you following a correct indentation? Consider pasting the edited code in your question and run it after fixing the bugs.
Now im getting, ValueError: invalid literal for int() with base 10: 'g', any ideas?
@user3080288 How about checking with isdigit() before using int()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.