Reputation: 45
I am making a little math game, similar to zeta mac. Everything seems to be working well. Ideally I would like this console output to erase incorrect answers entered by the user, without reprinting the math problem again for them to solve. Is something like this possible?
For example, I may prompt the user to answer "57 + 37 = " in the console, then if they type 24 (console would look like this "57 + 37 = 24", I would like the 24 to be erased, and for the "57 + 37 = " to remain, allowing the user to guess again, without the same equation having to be printed again on a line below.
Here is the source code (sorry if its messy, I just started learning python):
import random
import time
def play(seconds):
start_time = time.time()
score = 0
while True:
current_time = time.time()
elapsed_time = current_time - start_time
a = random.randint(2, 100)
b = random.randint(2, 100)
d = random.randint(2, 12)
asmd = random.choice([1, 2, 3, 4])
if (asmd == 1):
solve = a + b
answer = input("%d + %d = " % (a, b))
elif (asmd == 2):
if (a > b):
solve = a - b
answer = input("%d - %d = " % (a, b))
else:
solve = b - a
answer = input("%d - %d = " % (b, a))
elif (asmd == 3):
solve = a * d
answer = input("%d * %d = " % (a, d))
else:
solve = d
c = a * d
answer = input("%d / %d = " % (c, a))
while (solve != int(answer)):
answer = input("= ")
score += 1
if elapsed_time > seconds:
print("Time\'s up! Your score was %d." % (score))
break
play(10)
Upvotes: 2
Views: 145
Reputation: 117
Just use add these two lines after answer = input("= "):
sys.stdout.write("\033[F") #back to previous line
sys.stdout.write("\033[K") #clear line
import random
import time
import sys
def play(seconds):
start_time = time.time()
score = 0
while True:
current_time = time.time()
elapsed_time = current_time - start_time
a = random.randint(2, 100)
b = random.randint(2, 100)
d = random.randint(2, 12)
asmd = random.choice([1, 2, 3, 4])
if (asmd == 1):
solve = a + b
answer = input("%d + %d = " % (a, b))
elif (asmd == 2):
if (a > b):
solve = a - b
answer = input("%d - %d = " % (a, b))
else:
solve = b - a
answer = input("%d - %d = " % (b, a))
elif (asmd == 3):
solve = a * d
answer = input("%d * %d = " % (a, d))
else:
solve = d
c = a * d
answer = input("%d / %d = " % (c, a))
while (solve != int(answer)):
answer = input("= ")
if solve != int(answer):
sys.stdout.write("\033[F") #back to previous line
sys.stdout.write("\033[K") #clear line
score += 1
if elapsed_time > seconds:
print("Time\'s up! Your score was %d." % (score))
break
play(10)
Upvotes: 1
Reputation: 458
Removing characters from the end of the input is probably not the best approach as you aren't quite sure how many characters there will be (depends on the number).
The answer from @QualidSai does clear the terminal but it doesn't show your formula string again. To solve this I'd store the formula string that you print as part of your input as a string variable, then you can use that when looping. For example:
import random
import time
import sys
def play(seconds):
start_time = time.time()
score = 0
while True:
current_time = time.time()
elapsed_time = current_time - start_time
a = random.randint(2, 100)
b = random.randint(2, 100)
d = random.randint(2, 12)
asmd = random.choice([1, 2, 3, 4])
if (asmd == 1):
solve = a + b
question = "%d + %d = " % (a, b)
elif (asmd == 2):
if (a > b):
solve = a - b
question = "%d - %d = " % (a, b)
else:
solve = b - a
question = "%d - %d = " % (b, a)
elif (asmd == 3):
solve = a * d
question = "%d * %d = " % (a, d)
else:
solve = d
c = a * d
question = "%d / %d = " % (c, a)
answer = False
while (solve != int(answer)):
answer = input(question)
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
score += 1
if elapsed_time > seconds:
print("Time\'s up! Your score was %d." % (score))
break
play(10)
Upvotes: 1