python - How to keep random.choice() from rechecking itself -
today writing code in give addition or subtraction of integer problems. example:
from random import randint, choice while true: value_1 = randint(-9,9) value_2 = randint(-9,9) lol = choice(['+', '-']) number = input("what %s %s %s" % (value_1, lol, value_2)) if lol == "+": try: number_x = int(number) except valueerror: print("you did not enter number silly goose") else: if number_x == value_1 + value_2: print("you got answer right. job!!") else: print("incorrecto padwan. still have learn") elif lol == "-": try: number_x = int(number) except valueerror: print("you did not enter number silly goose") else: if number_x == value_1 - value_2: print("you got answer right. job!!") else: print("incorrecto padwan. still have learn") else: print("how did u this")
the issue when choice() function chooses "+" or "-", instead of adding or subtracting want when checking if person got answer correct, rechecks choice function. means if computer asks person "what 9 - 8", rechecks lol. instead of checking see if person got answer 9-8, computer checks see if person got right answer 9+8. want here after choice() function checked first time, keeps output, not recheck choice after computer asks question person originally. or there easier way write outputs same thing asking? way sorry question being written confusedly , if answer obvious(i'm new programming)
i think can try different approach. here mine reference:
import random op1, op2 = ['', '-'], ['+', '-'] nums = list('0123456789') if __name__ == '__main__': while true: question = ''.join([ random.choice(i) in (op1, nums, op2, nums) ]) print 'q: ' + question + ' =', answer = raw_input() if answer == 'q': print 'bye' break elif not answer.lstrip('-').isdigit(): print '%s not valid answer\n' % (answer) elif int(answer) == eval(question): print 'good job\n' else: print 'try again\n'
sample output:
q: -9+6 = 3 try again q: 0-6 = -6 job q: -3+3 = e e not valid answer q: 1-2 = q bye
Comments
Post a Comment