Skip to content Skip to sidebar Skip to footer

Randomly Generating Math Questions

My task is to produce a code that greets the user and asks their name storing their name as username. Then generates 2 random numbers and an operation. The question is asked to th

Solution 1:

You have a test while questionAsked > 11 but don't use that name anywhere else in your code. You certainly never defined it. You probably wanted to test questionsAsked (with an s) instead.

There are other problems, however. The loop should continue while you have fewer than 11 questions asked, not more. You also call test() twice, you should only call it once each loop. In your loop you use questionsAnswered but never defined that either and don't increment questionsAsked; you probably meant to increment the latter:

correctAnswers=0
questionsAsked=0while questionsAsked < 10:
    iftest():
        correctAnswers +=1
    questionsAsked +=1

Now test() is only called the once. Both your branches incremented questionsAsked, I moved that out of the tests, and now you no longer need to check if the test failed.

Since you start counting at zero, you want to test for < 10, not 11.

Instead of a while loop, you could use a for loop using the range() function:

for question_number in range(10):
    if test():
        correctAnswers +=1

Now the for loop takes care of counting the number of questions asked, and you no longer need to increment a variable manually.

Next, you need to move the username handling out of the test() function. You don't need to ask the user for their name each time. Ask for the name once, before the loop, so that you can access the user's name after the 10 questions:

deftest():
    num1=random.randint(1, 10)
    num2=random.randint(1, 10)
    # ... etc.


Username = input("What is your name?")
print("Welcome", Username, "to the Arithmetic quiz")

correctAnswers = 0for question_number inrange(10):
    if test():
        correctAnswers +=1# print the username and correctAnswers 

You need to be careful about your names in the test() function too; you define the names Ops and Operation but try to use them as ops and operation instead. That won't work, you need to use the same case everywhere to refer to those names. The Python style guide recommends you use all lowercase with underscores for local names, to distinguish them from class names (which use CamelCase, initial uppercase letters and no spaces between words).

Next problem: you are using str() with two arguments here:

print("What is" +" "+str(num1) + operation +str (num2,"?"))

That won't work; a two-argument str() call is meant for decoding bytes to a Unicode string.

Rather than use string concatenation, just pass your values to print() as separate arguments. The function will take care of converting things to strings and adds spaces between separate arguments for you:

print("What is", num1, operation, num2, "?")

Now there will be a space between num2 and the "?" but that is not that big a problem. You can use the str.format() method to create a string with placeholders where arguments to the method are filled in for you, again converting to strings automatically. This allows you to control spaces more directly:

print("What is {} {} {}?".format(num1, operation, num2))

The three arguments are placed where each {} appears, in order.

Solution 2:

You have many discrepancies in variable names and indentation. Remember Python is case sensitive. And by the way, the condition in your while loop will cause your program not to ask any questions.

So for example, you created a list of operations called Ops and then used the random module to select an operation from ops. Python will inevitably throw an error, as ops is not actually defined. Instead, you should use Ops because that is the variable you actually declared, with a capital letter. Again, Python is case sensitive.

Likewise, Python recognizes a difference between questionAsked and questionsAsked. It's one or the other, so choose a name and be consistent.

Post a Comment for "Randomly Generating Math Questions"