Join 136,825 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!
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:
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
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?
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.
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:
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 ... '''
CODE
# 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 Sep, 2008 - 06:18 PM
Thanks that helped me a lot. I made it totally different though because I want a random x and y.
CODE
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 Sep, 2008 - 04:49 PM
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 ...
CODE
# 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