patternpythonMinor
Simple multiplication quiz
Viewed 0 times
quizsimplemultiplication
Problem
I am learning Python, and would like to learn how to make this code simpler and easier to read.
Things I am hoping to learn:
Things I am hoping to learn:
- What variables might be appropriate?
- How can I make it simpler?
- Is there any way to make it more efficient?
- What can be put into functions without sacrificing readability?
import random
yn = raw_input("Do you wanna build a mathman? y/n ")
if yn == "n":
exit()
else:
while yn =="y":
x =int( random.choice(range(1,100)))
y =int( random.choice(range(1,100)))
a = str(x)
b = str(y)
s = 0
xy = x * y
print "What is " + a + " x " + b + "?"
ua = int(raw_input("What is the answer? "))
while ua != xy:
print "You are wrong, try again "
ua = int(raw_input("Try again- "))
s = s - 1
yn = raw_input("You are correct, would you like to go again? y/n ")
s = s + 1
t = str(s)
print "Your score is " + t
Solution
First of all, you have the following construct:
But if
This is like an 'early return' (except not in a function, but in the main body of code).
Second, you have
The spacing on the line with
That is: no spaces next to
Furthermore, you should really use string formatting, allowing you to write
This way you don't need intermediate variables
I assume
Why not just write
You're basically asking a question, and then "What is the answer to the question?". Better to just ask the question alone.
Instead of writing
Another thing of note: the score (I assume that's what you mean with
That is, you have (pseudo-code)
While instead I think you mean
Note that the last two lines can be replaced with
(Also note: if the user inputs something different from
if yn == "n":
exit()
else:
lots
of
codeBut if
yn equals "n", the exit() will have been executed, and you won't reach any other lines anyway. So you can just writeif yn == "n":
exit()
lots
of
codeThis is like an 'early return' (except not in a function, but in the main body of code).
Second, you have
x =int( random.choice(range(1,100)))
...
a = str(x)
...
print "What is " + a + " x " + b + "?"The spacing on the line with
x = is off. It should bex = int(random.choice(range(1, 100))That is: no spaces next to
( and ). A space on each side of the =, and a space after the ,.Furthermore, you should really use string formatting, allowing you to write
print "What is {} + {}?".format(x, y)This way you don't need intermediate variables
a and b.I assume
ua means user_answer here:print "What is " + a + " x " + b + "?"
ua = int(raw_input("What is the answer? "))Why not just write
problem = "What is {} x {}? ".format(x, y)
user_answer = int(raw_input(problem))You're basically asking a question, and then "What is the answer to the question?". Better to just ask the question alone.
Instead of writing
xy, you could write solution or answer.Another thing of note: the score (I assume that's what you mean with
s) gets reset to 0 each time you get a new problem statement, but the score (t) is only printed at the end of the rounds.That is, you have (pseudo-code)
while play_again:
...
s = 0
answered = ...
while answered != solution:
answered = ...
s -= 1 # You had s = s - 1, that is equivalent to s -= 1
s += 1
t = str(s)
print "Your score is " + tWhile instead I think you mean
s = 0
while play_again:
...
answered = ...
while answered != solution:
answered = ...
s -= 1 # You had s = s - 1, that is equivalent to s -= 1
s += 1
t = str(s)
print "Your score is " + tNote that the last two lines can be replaced with
print "Your score is {}".format(s)(Also note: if the user inputs something different from
y or n on the first go, you get a NameError: t is referenced before it is assigned because you never enter the while loop).Code Snippets
if yn == "n":
exit()
else:
lots
of
codeif yn == "n":
exit()
lots
of
codex =int( random.choice(range(1,100)))
...
a = str(x)
...
print "What is " + a + " x " + b + "?"x = int(random.choice(range(1, 100))print "What is {} + {}?".format(x, y)Context
StackExchange Code Review Q#117879, answer score: 6
Revisions (0)
No revisions yet.