# Python number combination

Page 1 of 1

## 5 Replies - 3353 Views - Last Post: 24 September 2008 - 10:49 PMRate 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=64354&amp;s=9970decf8a996d05519e8750f961a65d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kbiz

Reputation: 0
• Posts: 13
• Joined: 18-September 08

# Python number combination

Posted 18 September 2008 - 07:11 PM

Hi, I am trying to get better at python so I want to make what should be an easy program. I am trying to get it to print all 3 number combinations for a lock. I subtracted forty because lock numbers only go up to thirty-nine. I also get a weird output that I don't understand.
here's the code:

```
s = int
f = int
thirdnum = 29
def firstnum():
for f in range(25, 4, 61):
if f < 40:
print f
else:
print f - 40
break

def secondnum():
for s in range (25, 2, 63):
if s < 40:
print s
else:
print s - 40
break

def combo():
firstnum, secondnum, thirdnum

def loop():
while secondnum != firstnum:
print combo
print loop

```

Is This A Good Question/Topic? 0

## Replies To: Python number combination

### #2 csmanoj

Reputation: 11
• Posts: 150
• Joined: 06-August 07

## Re: Python number combination

Posted 20 September 2008 - 10:29 AM

The indentations are not right. Correct them. But that's all I can find there. I don't even understand what you're trying to do. Could you explain it a bit better so I can help?

### #3 kbiz

Reputation: 0
• Posts: 13
• Joined: 18-September 08

## Re: Python number combination

Posted 20 September 2008 - 02:13 PM

All i am trying to do is have the program list a first number and a second number within the range counting by the middle number, and I don't want it to print when the first number equals the second number. I just put the third number as 29. So I want it to start printing with
25, 27, 29 where 25 is the first number and 27 is the second number. then it would print 29, 25, 29... all the way through the range and when the first number gets to 40, what would be like 41, 39, 29 would be 0, 39, 29. So I just want the program to keep printing all the way through the range. The program doesn't have much of a purpose, I just want to see it work. hope this helped.

### #4 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Python number combination

Posted 20 September 2008 - 10:22 PM

kbiz, on 18 Sep, 2008 - 07:11 PM, said:

Hi, I am trying to get better at python so I want to make what should be an easy program. I am trying to get it to print all 3 number combinations for a lock. I subtracted forty because lock numbers only go up to thirty-nine. I also get a weird output that I don't understand.
here's the code:

```
s = int
f = int
thirdnum = 29
def firstnum():
for f in range(25, 4, 61):
if f < 40:
print f
else:
print f - 40
break

def secondnum():
for s in range (25, 2, 63):
if s < 40:
print s
else:
print s - 40
break

def combo():
firstnum, secondnum, thirdnum

def loop():
while secondnum != firstnum:
print combo
print loop

```

Quote

'''
All i am trying to do is have the program list a first number and a second number within the range counting by the middle number, and I don't want it to print when the first number equals the second number. I just put the third number as 29. So I want it to start printing with 25, 27, 29 where 25 is the first number and 27 is the second number. then it would print 29, 25, 29... all the way through the range and when the first number gets to 40, what would be like 41, 39, 29 would be 0, 39, 29. So I just want the program to keep printing all the way through the range. The program doesn't have much of a purpose, I just want to see it work ...
'''

```# try this ...

y = 27
for x in range( 25, 61, 4 ):
if x < 40:
print x, y, 29
else:
print x-41, y+4, 29 # note gives your above 0,39,29
y +=2

# or maybe you really wanted the following
# where the 2nd number grows by 2 each time

print
y = 27
formatStr = "%2d, %2d, %2d"
for x in range( 25, 61, 4 ):
if x < 40:
print formatStr % (x, y, 29)
else:
print formatStr % (x-41, y, 29)
y +=2

# Note: if you want the value for x=61
# to be considered, then change your code
# from ...
# for x in range( 25, 61, 4 ):
# to ...
# for x in range( 25, 62, 4 ):

```

This post has been edited by David W: 21 September 2008 - 07:18 PM

### #5 kbiz

Reputation: 0
• Posts: 13
• Joined: 18-September 08

## Re: Python number combination

Posted 24 September 2008 - 05:47 PM

Thanks that helped me a lot. I made it totally different though because I want a random x and y.
```import random

def numbers():
y =  random.randrange(25, 62, 2)
x =  random.randrange(25, 62, 4)
if x < 40 and y < 40 and x!=y:
print x, y, 29
if x > 40 and y > 40 and x!=y:
print x - 40, y - 40, 29
if x < 40 and y > 40 and x!=y:
print x, y - 40, 29
if x > 40 and y < 40 and x!=y:
print x - 40, y, 29

numbers()
```

This looks weird but it works, except it only prints one set of numbers
at a time and im not sure how to loop it.

This post has been edited by kbiz: 24 September 2008 - 05:49 PM

### #6 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Python number combination

Posted 24 September 2008 - 10:49 PM

kbiz, on 24 Sep, 2008 - 05:47 PM, said:

Thanks that helped me a lot. I made it totally different though because I want a random x and y. ... This looks weird but it works, except it only prints one set of numbers at a time and im not sure how to loop it.

Try this loop then ...

```# loopingOutputHelp.py

import random
random.seed();

def numbers():
y =  random.randrange(25, 62, 2)
x =  random.randrange(25, 62, 4)
if x < 40 and y < 40 and x!=y:
return x, y, 29
if x > 40 and y > 40 and x!=y:
return x - 40, y - 40, 29
if x < 40 and y > 40 and x!=y:
return x, y - 40, 29
if x > 40 and y < 40 and x!=y:
return x - 40, y, 29
else:			   # if none of the above  ... then
return -9,-9,-9 # need to return 3 'int's

for runNum in range (99):
a, b, c = numbers()
print "Run(%2d) = (%2d, %2d, %2d)  " % ( runNum+1, a, b, c ),
if (runNum+1) % 3 == 0:
print

```