New to Python

This is the place for queries that don't fit in any of the other categories.

New to Python

Postby InkGlass » Thu Jun 06, 2013 12:55 pm

Hello, I have started learning Python and I am not a programmer. It is only my second day. Also this is not for homework, I am just trying to learn how to program. So I have written a simple program, which works, just not all the time. The objective of the program is to generate a random number either weighted high or weighted low, within a specific range. When it works it prints the number, problem is sometimes it doesn't print anything and I have no idea why. I would really appreciate it if someone could help me work out what is going wrong, thank you for your help in advance! So I am using Python 3.1 (because it seems to have more tutorials) in windows 7. Here is the program:
Code: Select all
import random
def lownum():
        zar = random.randint(1, 27)
        print (zar)
       
def hinum():
        par = random.randint(27, 54)
        print (par)
       
way = random.randint (1, 10)
if way < 5:
        lownum()
if way > 5:
        hinum()
       
InkGlass
 
Posts: 3
Joined: Thu Jun 06, 2013 12:42 pm

Re: New to Python

Postby MichelFJM » Thu Jun 06, 2013 2:41 pm

Hello
What should happen if way=5 ?
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm

Re: New to Python

Postby InkGlass » Thu Jun 06, 2013 3:06 pm

MichelFJM wrote:Hello
What should happen if way=5 ?


Hmm I guess the program wont print if its 5, lol! Thanks! Though aside from that the program doesn't actually work as I expected even when the 5 is fixed, so I tried to come up with something new:

Code: Select all
  import random
way = random.randint (1, 1000)

def lownum():
        zar = random.randint(1, 29)
        print (zar)
def hinum():
        par = random.randint(30, 60)
        print (par)
def gen():
        if way < 500:
              lownum()
        else: hinum()

slot = 0
       
while slot < 6:
        gen()
        slot = slot + 1                                                                   



I was hoping the numbers would alternate between the lownum and the hinum function, instead all six numbers are picked from lownum or all six numbers are picked from the hinum but there is no mixing of lownum & hinum numbers, obviously my knowledge is severely limited at this time... Thank you for the help I greatly appreciate it!
InkGlass
 
Posts: 3
Joined: Thu Jun 06, 2013 12:42 pm

Re: New to Python

Postby stranac » Thu Jun 06, 2013 3:25 pm

You're only creating way once.
Since it doesn't change, the same function is called every time.
You should move it's creation into your gen() function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: New to Python

Postby InkGlass » Thu Jun 06, 2013 3:51 pm

stranac wrote:You're only creating way once.
Since it doesn't change, the same function is called every time.
You should move it's creation into your gen() function.



Thank you! That worked perfectly. I had thought that every time a function was accessing "way" that it would generate a new number. Much appreciated.
InkGlass
 
Posts: 3
Joined: Thu Jun 06, 2013 12:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron