# 24 game regular expressions and input error

Page 1 of 1

## 4 Replies - 1177 Views - Last Post: 08 July 2011 - 06:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=238552&amp;s=d0b4c7ab2f0572c918b11e722c681435&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

# 24 game regular expressions and input error

Posted 07 July 2011 - 07:14 PM

```
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
if search:
return True
else:
return False
def main():
digits = num4()
trys = 1
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()
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.

Thanks!

This post has been edited by ipushmycar: 07 July 2011 - 07:39 PM

Is This A Good Question/Topic? 0

## Replies To: 24 game regular expressions and input error

### #2 Eric115

• coderさん

Reputation: 63
• Posts: 698
• Joined: 19-January 09

## Re: 24 game regular expressions and input error

Posted 07 July 2011 - 09:46 PM

Hi there, I went over your code and patched it up. I am not sure what version of python you are running, so you might need to change some of the syntax but I kept it as close as I could to the original.
```import random, re

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) #you had random,randint instead of random.randint (, instead of .)

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

#re is a string module
str_digits = str(digits)#make these strings or they cant be searched
if search:
return True
else:
return False

def main():
digits = num4()
trys = 1
equ = False
while trys < 10 and not (equ == 24):
answer = input("Exp<b></b>ression %i: " % trys)
if str_answer.startswith('q'): #this returns true if it startswith q
exit(0)
elif str_answer.startswith('!'): #use elif for multiple if statements
#until you need to check for something different, then go back to if
#using ! as my input caused an EOF error for me, but see how you go with it, maybe use a different character?
digits = num4()

stringNumbs = str(answer)#sometimes better to make the string here
equ = eval(stringNumbs) #then pass it to the function
if equ == 24 and numbers(digits,answer):
print "You got it in only " + str(trys) + " try(s)!"
trys +=1
main()

```

I made comments next to things I changed and what should happen now. The game worked for me so hopefully it will work for you.
happy coding!

This post has been edited by Eric115: 07 July 2011 - 09:49 PM

### #3 Nallo

• D.I.C Regular

Reputation: 163
• Posts: 257
• Joined: 19-July 09

## Re: 24 game regular expressions and input error

Posted 08 July 2011 - 03:55 AM

The error you get from inputing q or ! stems from the following:
In python 2.x
```a = input("expr: ")

```

is equivalent to
```a = eval(raw_input("expr: "))

```

using input already evaluates the expression, while raw_input gives the expression back as a string:
```>>> a = input("expr :")
expr :3+3
>>> a
6
>>> a = raw_input("expr :")
expr :3+3
>>> a
'3+3'

>>> a = input("expr :")
expr :q
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
a = input("expr :")
File "<string>", line 1, in <module>
NameError: name 'q' is not defined

```

In the last example python tries eval("q") but as q is not a variablename it is not a legal expression. And causes an error.

From the way your programm looks I think you really wanted to use raw_input instead of input.

### #4 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

## Re: 24 game regular expressions and input error

Posted 08 July 2011 - 05:59 AM

Ah, okay thanks guys! The , was just a type in my copying over of my code but other than that I will implement the changes and test it.

### #5 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

## Re: 24 game regular expressions and input error

Posted 08 July 2011 - 06:29 AM

Haha, Well I just tried typing in 24 with the numbers 2 4 4 7 and it accepted it. For this self project I think I am done, maybe later I will come back and edit it.