def num4(): 'find 4 digits that are greater than 0' digits = [1,1,1,1] for i in range(4): digits[i] = random,randint(1,9) print "The four number you have to work with are: " print digits print "Your goal is to use addition, multiplication, subtraction, or divison to get 24, all 4 numbers must be used." return digits def numbers(digits,answer): search = re.search(digits,answer) if search: return True else: return False def main(): digits = num4() trys = 1 answer = "" equ = False while trys < 10 and not (equ == 24): answer = input("Expression %i: " % trys) if str(answer).lower() == 'q': exit(0) if str(answer) == '!': digits = num4() equ = eval(str(answer)) if equ == 24 and numbers(digits,answer): print "You got it in only " + str(trys) + " try(s)!" trys +=1 main()
Hey guys I have been teaching my self python and started to program the 24 game. I got it to work but then wanted to test to make sure they use all four digits not 3 or 2 or simply just inputted the number '24'. I figured regular expressions would be perfect for this but I have only ever used them in java and c++ with the boost libraries. I get an error while trying to search for the digits in the answer I get from the user. I also am getting errors when I test to see if the user input equals a ! or a q. The ! should give them a new set of numbers to work with and q should exit the program. Any tips and/or suggestions would be greatly appreciated.
This post has been edited by ipushmycar: 07 July 2011 - 07:39 PM