Home » 2016 » March » 23 » Randomly generating math questions - Stack Overflow
3:21 PM
Randomly generating math questions - Stack Overflow





#ask maths questions #

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:

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:

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:

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:

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:

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:

The three arguments are placed where each <> appears, in order.



Views: 256 | Added by: b0ss_putuxyyj56 | Tags: generating, Stack, Math, Questions, Randomly, Overflow | Rating: 0.0/0
Total comments: 0
avatar
Site menu
Log In
ADSENSE
Calendar
«  March 2016  »
SuMoTuWeThFrSa
  12345
6789101112
13141516171819
20212223242526
2728293031
ADSENSE
Site friends
Statistics

Total online: 392
Guests: 392
Users: 0