Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,687 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,270 people online right now. Registration is fast and FREE... Join Now!




Python number combination

 
Reply to this topicStart new topic

Python number combination

kbiz
post 18 Sep, 2008 - 06:11 PM
Post #1


New D.I.C Head

*
Joined: 18 Sep, 2008
Posts: 3


My Contributions


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



User is offlineProfile CardPM

Go to the top of the page

csmanoj
post 20 Sep, 2008 - 09:29 AM
Post #2


D.I.C Head

Group Icon
Joined: 6 Aug, 2007
Posts: 139



Thanked 3 times

Dream Kudos: 50
My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page

kbiz
post 20 Sep, 2008 - 01:13 PM
Post #3


New D.I.C Head

*
Joined: 18 Sep, 2008
Posts: 3


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

David W
post 20 Sep, 2008 - 09:22 PM
Post #4


D.I.C Regular

Group Icon
Joined: 20 Sep, 2008
Posts: 308



Thanked 15 times

Dream Kudos: 250
My Contributions


QUOTE(kbiz @ 18 Sep, 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:

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
User is offlineProfile CardPM

Go to the top of the page

kbiz
post 24 Sep, 2008 - 04:47 PM
Post #5


New D.I.C Head

*
Joined: 18 Sep, 2008
Posts: 3


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

David W
post 24 Sep, 2008 - 09:49 PM
Post #6


D.I.C Regular

Group Icon
Joined: 20 Sep, 2008
Posts: 308



Thanked 15 times

Dream Kudos: 250
My Contributions


QUOTE(kbiz @ 24 Sep, 2008 - 05:47 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


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:21AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month