Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning Python
#1
I decided to learn Python. Yesterday I was reading about for and while loops. The tutorial ended with an assignment of making a lottery picker. So, I did. Probably much fast and cleaner ways to do it, but here is what I came up with, using both a for loop and a while loop. The wife and kids don't care, had to show someone, so here it is.
import random
import sys
numbers=[]

def skip ():
   print("\n")
   
   
def pick():
   skip()
   skip()
   skip()
   skip()
   skip()

   while len(numbers) < 5:
       pick=random.choice(range(1,69))
       if pick in numbers:
           pick=random.choice (range (1,69))
       else:
           numbers.append(pick)
   
                                
   pball=random.choice(range(1,26))
   num=sorted(numbers)
   skip() 
   print ("\t  ", end=' ')
   
   count=0
   for i in range(len(num)):
       print("", end=' ')
       print(num[0+count], end=' ')
       count+=1
   
   skip()
   print("\t     Power Ball = ", end=' ')
   print(pball)
   skip()
   skip()
   skip()
   pickem()

def pickem():
   again=input("\tENTER To Pick Or Q To Quit - \n\n")
   again=again.lower()
   if again.startswith("q"):
       sys.exit
   else:
       numbers[:]=[]
       pick()

pickem() 
Reply
#2
Cool!  I have some suggestions, and also a bugfix for you :)

def pickem():
   again=input("\tENTER To Pick Or Q To Quit - \n\n")
   again=again.lower()
   if again.startswith("q"):
       sys.exit
   else:
       numbers[:]=[]
       pick()
"sys.exit" does nothing if you don't actually call it.  But since you use an "else", your program doesn't actually do anything anyway, which is why it looks like it works.  I'd suggest getting rid of sys.exit entirely (it's kind of a dirty way to end your program), and instead only doing things when you actually want to do things.  Like this:
def pickem():
   again=input("\tENTER To Pick Or Q To Quit - \n\n")
   again=again.lower()
   if not again.startswith("q"):
       numbers[:]=[]
       pick()
Next, "numbers[:] = []" is an interesting way to erase the numbers.  ...especially since that function doesn't actually use that variable in any way.  Why not just "numbers = []" instead?  ...and then, actually, just don't touch it at all there, since you don't even use it there (separation of concerns will help you make larger programs easier).  So instead of having "numbers = []" at the very top of your script, you can have it inside the function it's actually used...
def pick():
  numbers = []
  skip()
  # ...etc
That way, it's redefined as an empty list inside the only function that uses it, and you have one less global variable, so your code is cleaner, makes a little more sense to the casual viewer, and is less error prone.

pickem() calls pick(), which then calls pickem() which could call pick()... it's an infinite loop (maybe)!  Remember, separation of concerns is good, so let's change that a bit.  pick() does one thing... it picks lotto numbers.  It shouldn't care who asked it to pick those numbers, or where it came from, or why you want it, it just does it's job and then gives up control of the script to whoever called it.  So the first step is getting rid of the "pickem()" at the end of pick().  Then wrap the contents of pickem() inside a while loop, so you can keep calling pick() over and over if you want.  With the changes from before, it'd look a little like this:
def pickem():
   running = True
   while running:
       again = input("\tENTER To Pick Or Q To Quit - \n\n").lower()
       if again.startswith("q"):
           running = False
       else:
           pick()
Reply
#3
Thank you! This is exactly what I need to continue learning. I am very new at this and your suggestions are very sppreciated. I will implement them at work today and make sure I go over it enough to understand it. Luckily I can do Python on my android phone on my downtime.

I found some free time and implemented your suggested changes. Your explanations made perfect sense. I think it helped that they were related specifically to something I wrote. Tonight at work I am going to find a new small project to write using what I have learned here.

Luckily I have Python on my smart phone. The only downside is things like print("\n") are a pain to type on my phone. Thankfully functions let me change it to something that types easily like skip()

Again, thank you for taking the time to write those suggestions and and explanations. I am better at Python than I was this morning, and that is because of your efforts.
Reply
#4
(Oct-18-2016, 04:05 PM)LordHill Wrote: Luckily I have Python on my smart phone. The only downside is things like print("\n") are a pain to type on my phone.
Thats why programmers use desktops, not phones. Its "cool", but not practical. OF course you could use a phone to test an .apk etc. but actually writing the code for programs on a phone would be a nightmare.
Recommended Tutorials:
Reply
#5
Also, you never need to do print('\n'). print() without any arguments gives you a newline :p
Reply
#6
When I am at home I use my 2 desktops, my laptop, and my raspberry pi. When I am at work those options are off the table obviously, but I can still use that time to learn thanks to my phone.

Good to know about print() tho.
Reply
#7
(Oct-18-2016, 07:41 PM)LordHill Wrote: When I am at home I use my 2 desktops, my laptop, and my raspberry pi. When I am at work those options are off the table obviously, but I can still use that time to learn thanks to my phone.

Good to know about print() tho.

what kind of work do you do?  truck driver?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
I also want to add something. In the following code
   count=0
   for i in range(len(num)):
       print("", end=' ')
       print(num[0+count], end=' ')
       count+=1
You don't need count. You can use the from the for loop to the same effect. However that's not pythonic. The idiom to iterate over elements in a list is like this

my_list=[1,2,3]
for n in my_list:
    print(n)
Output:
1 2 3
I also don't like defining your own function skip. All it does is to call the built-in print function and this way it obfuscate what's really going on (i.e. it's more lines of code and more difficult for someone reading your code). Instead of calling skip elsewhere in your code you can just call the print function. Eventually if you still want to have own function, you can do something like this
def skip(n=1):
    print('\n'*n)
this way you have function that with a single call prints arbitrary number n of new lines, where n is supplied as an argument. Of course, still the better way would be to use that print from the body of the above skip function directly in the code, without defining new function.
Reply
#9
(Oct-19-2016, 12:32 AM)Skaperen Wrote: what kind of work do you do?  truck driver?

I am a Maintenance Crew Leader in a sugar factory.

TThanks for the input! I appreciate it. Those both make sense
Reply
#10
Just wanted to say thanks again for you folks input. I find myself using your pointers to write my code now and I notice myself not making those same mistakes that I was making. I'm sure I am making plenty of new mistakes, but the stuff you guys told me here made perfect sense and clicked. 

Since this I have made a hangman game, a craps game, a tkinter program for work to log information on pumps (we have over 200 and each take different parts), and today I found myself staring at a file with 11,000+ files in it that needed sorted. Threw together a sorter that got me the 500 or so files I needed. 

Anyway, I appreciate your input. Keep up the good work

*folder .. not file
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020