Python Forum
Requesting feedback on this learning script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Requesting feedback on this learning script
#1
Hello,
I'm just beginning to teach myself python. This is one of my toy programs for learning and I would really appreciate feedback to gauge things like style, efficiency, logic, etc...

Also, I was wondering if there is a way to use a list comprehension for the statements in the functions (make_close_under & make_veryclose_under).

Thank you in advance for your time and help!

#!/usr/bin/env python

####################
""" DATE: 2017Nov29
    PYTHON: 2.7.5
    OS: RHEL 7.4 X86_64
    AUTHOR: I_love_py
    PURPOSE: Guess which number the program selected
    DETAILS:
          --> validate the user's input to be a number
          --> give clues when the user guesses about whether too high or too low
          --> give praise if they guess it with in 3 tries
          --> give message when they get it correct
"""

import random
import sys

""" create range for sequence """
startnum = 1
endnum = 101

""" use these lists below will be used to create sequence of numbers
    in a  list, that are close / very close to the prog_num value
"""
clist = [6,5,4] # for making close list
vclist = [3,2,1] # for making very close list

def getRandomnum():
   """ generates random number in specifed range """
   return (random.choice([r for r in range(startnum,endnum)]))

""" The 4 functions below will be used to generate a list that
    will help determine if the guess number is near the program number.
    --> make_close_under()
    --> make_veryclose_under()
    --> make_close_above()
    --> make_veryclose_above()
"""


def make_close_under(prog_num):
   """ Make a list that is 6,5 or 4 below the
       number that the program chose.
   """
   mylist = list()
   for i in clist:
      newnum = prog_num-i
      if not newnum < 0:
         mylist.append(newnum)
      else:
         mylist.append(0)
   return mylist

def make_veryclose_under(prog_num):
   """ Make a list that is 3,2 or 1 below the
       number that the program chose.
   """
   mylist = list()
   for i in vclist:
      newnum = prog_num-i
      if not newnum < 0:
         mylist.append(newnum)
      else:
         mylist.append(0)
   return mylist

def make_close_above(prog_num):
   """ Make a list that is 6,5 or 5 above the
       number that the program chose.
   """
   return [prog_num+i for i in clist]


def make_veryclose_above(prog_num):
   """ Make a list that is 3,2 or 1 above the
       number that the program chose.
   """
   return [prog_num+i for i in vclist]


def main():
    """ assign the random number to variable """
    prog_num = getRandomnum()

    """ create the lists for number comparisons """
    close_under = make_close_under(prog_num)
    veryclose_under = make_veryclose_under(prog_num)
    close_above = make_close_above(prog_num)
    veryclose_above = make_veryclose_above(prog_num)

    print("\nGuess which number the program has selected. (Range between {0} & {1})".format(startnum,endnum-1))
    tries = 1
    while True:
       user_guess = raw_input("Take a guess (type quit to leave): ")
       if user_guess.lower() in ['quit']:
          print("Thanks for playing. Goodbye")
          sys.exit()
       try:
          int(user_guess)
       except ValueError as e:
          print("\n\tERROR: Sorry, only integers accepted.")
       else:
          if (int(user_guess) >= endnum):
             print("\nUMM...since {0} is out of range, you definitely need to go lower.".format(user_guess))
          elif (int(user_guess) <= startnum):
             print("\nUMM...since {0} is out of range, you definitely need to go higher.".format(user_guess))
          elif (int(user_guess) in veryclose_under):
             print("\n{0} is very close, try higher.".format(user_guess))
          elif (int(user_guess) in close_under):
             print("\n{0} is close, try higher.".format(user_guess))
          elif (int(user_guess) in veryclose_above):
             print("\n{0} is very close, try lower.".format(user_guess))
          elif (int(user_guess) in close_above):
             print("\n{0} is close, try lower.".format(user_guess))
          elif (int(user_guess) < prog_num):
             print("\ntry higher.".format(user_guess))
          elif (int(user_guess) > prog_num):
             print("\ntry lower.".format(user_guess))
          elif (int(user_guess) == prog_num ):
             if tries <= 3:
                separator = "#"*3
                print("{0}\nwhoa....you are amazing!!!! You guessed it in 3 tries or less.\n{1}\n".format(separator,separator))
             print("\n\tWINNER! WINNER! CHICKEN DINNER! Your guess {0} matches my number {1}.".format(user_guess,prog_num))
             print("\tYou did it in {0} tries.".format(tries))
             sys.exit()
          else:
             print("\n\t something isn't working")
             sys.exit()
       tries = tries + 1

if __name__ == "__main__":
    main()
Reply
#2
You can use pep8online to check your code for style issues.
Reply
#3
@RickyWilson - That's helpful, thank you very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My First Python Script. Feedback Sought. malonn 5 3,811 Jun-29-2018, 03:49 PM
Last Post: Zombie_Programming

Forum Jump:

User Panel Messages

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