Learn Python in One Day and Learn It Well (2015)
Part 2: mathGame.py
Congratulations for completing Part 1 and welcome to Part 2. Part 2 is going to be a breeze as we’ll mainly just be calling the functions we defined earlier.
Exercise 5: Writing the Main Program
First, let’s enclose our main program in a try, except statement. We want to handle any unforeseen errors when running the main program.
We’ll start by writing the code for the try block.
Firstly, we need to import the myPythonFunctions module. Next, let’s prompt the user for his/her username and assign the value to the variable userName. Pass this variable as a parameter to the function getUserScore().
getUserScore() will either return the score of the user or return ‘-1’ (if the user is not found). Let’s cast this result into an integer and assign it to the variable userScore.
Now, we need to set the value of another variable newUser. If the user is not found, newUser = True, else newUser = False. If newUser = True, we need to change userScore from -1 to 0.
The next part of our program involves a while loop. Specifically, our program will prompt for input from our user to determine if it should terminate the program or do something else.
You need to declare another variable userChoice and give it an initial value of 0.
Next, using a while loop, compare userChoice with a string of your choice, say “-1”. If userChoice is not the same as “-1”, call the function generateQuestion() to generate a new question.
generateQuestion() will return the score that the user got for that question. Use this result to update the variable userScore.
Finally, in order to prevent an infinite loop, we need to use the input() function again within the while loop to accept user input and use it to update the value of userChoice.
Got that? Try coding it. Doing the actual coding will make everything clearer.
Finally, after the while loop terminates, the next step is to update the userScores.txt file. To do that, we simply call the updateUserPoints() function.
That’s all for the try block. Now for the except block, we simply inform the user that an error has occurred and the program will exit.
That’s it! Once you finish this step, you’ll have a complete program, your first program in Python. Try running the program mathGame.py. Does it work as expected? Excited? I sure hope you are as excited about it as I am. :)
We’ve come to the end of this chapter and hopefully you have successfully coded your first program. If you have problems completing any exercise, you can study the answers in Appendix E. You will learn a lot by studying other people’s codes.
In this section, I have three additional exercises for you to challenge yourself.
Challenge Exercise 1
In the program that we’ve coded so far, I’ve avoided using the division operator. Can you modify the program so that it’ll generate questions with the division sign too? How would you check the user’s answer against the correct answer?
Hint: Check out the round() function.
Challenge Exercise 2
Sometimes, the question generated may result in an answer that is very large or very small. For instance, the question 6*(8^9/1)^3 will give the answer 1450710985375550096474112.
It is very inconvenient for users to calculate and key in such a large number. Hence, we want to avoid answers that are too big or small. Can you modify the program to prevent questions that result in answers greater than 50 000 or smaller than -50000?
Challenge Exercise 3
The last challenge exercise is the most difficult.
So far, brackets are missing in the questions generated. Can you modify the program so that the questions use brackets too? An example of a question will be 2 + (3*7 -1) + 5.
Have fun with these exercises. The suggested solution is provided in Appendix E.
We’ve come to the end of the book. Thank you for reading this book and I hope you have enjoyed the book. More importantly, I sincerely hope the book has helped you master the fundamentals of Python programming.
I know you could have picked from a dozen of books on Python Programming, but you took a chance with this book. Thank you once again for downloading this book and reading all the way to the end. Please do try the exercises and challenges. You’ll learn a lot by doing.
Now I’d like to ask for a “small” favor. Could you please take a few minutes or two to leave a review for this book on Amazon?
This feedback will help me tremendously and will help me continue to write more guides on programming. If you like the book or have any suggestions for improvement, please let me know. I will be deeply grateful. :)
Last but not least, remember you can download the source code for the project and the appendices at http://www.learncodingfast.com/python.
You can also contact me at firstname.lastname@example.org.