Null Value

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

Null Value

Postby dday9 » Mon Oct 14, 2013 4:32 pm

What is a Null or Nothing value in Python 3.3.2? I searched the Library and found the None constant, is this it? I'm looking for something similar to this:
Code: Select all
'Vb.net Code
Console.WriteLine("Please enter the last name. To return to the search menu, enter 'return'")
Dim str As String = Console.ReadLine()

If str = "return" Then
     Return Nothing
Else
     Return str
End If


Would that be this:
Code: Select all
#Python code
print ("Please enter the last name. To return to the search menu, enter 'return'")
if input() == "return":
        return None
else:
        return input()
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Null Value

Postby stranac » Mon Oct 14, 2013 4:45 pm

Yes.
And if you don't return a value from your function, None will be returned implicitly.

Also, there is no need for a separate print, you can give a prompt string to the input function.
Friendship is magic!

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

Re: Null Value

Postby dday9 » Mon Oct 14, 2013 4:49 pm

Also, there is no need for a separate print, you can give a prompt string to the input function.


Is that why I'm having to enter in the value twice? Because currently this is the full function of a different(longer) function:
Code: Select all
def sub_add_customer():
        foo_customer = customer
        print ("If at any time you wish to exit without saving, type 'return'")
        print ("Please enter in the new customer's name:")
        if input() == "return":
                return None
        else:
                foo_customer.name = input()
               
        print ("Please enter in the new customer's date of birth:")
        if input() == "return":
                return None
        else:
                foo_customer.dob = input()
               
        print ("Please enter in the new customer's address:")
        if input() == "return":
                return None
        else:
                foo_customer.address = input()
               
        print ("Please enter in the new customer's city:")
        if input() == "return":
                return None
        else:
                foo_customer.city = input()
               
        print ("Please enter in the new customer's state:")
        if input() == "return":
                return None
        else:
                foo_customer.state = input()
               
        print ("Please enter in the new customer's zip:")
        if input() == "return":
                return None
        else:
                foo_customer.zip = int(input())

        return foo_customer


But whenever I debug the program it I'm having to enter in the input value twice.
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Null Value

Postby stranac » Mon Oct 14, 2013 5:05 pm

That's not why.
Code: Select all
if input() == "return":
                return None
        else:
                foo_customer.name = input()

Here you're getting the user's input, checking if it's 'return', and if it's not you're getting a second user input and assigning it to foo_customer.name

You should instead store the input and use the stored value:
Code: Select all
name = input()
if name == "return":
                return None
        else:
                foo_customer.name = name


Also, that function just looks wrong...
  • foo_customer should be provided as an argument to the function, instead of using a global to assign it.
  • Initializing an object should be the job of the __init__() method, not some random function
  • You're repeating yourself all over the place

And please use 4 spaces for indentation, 8 is too much.
Friendship is magic!

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

Re: Null Value

Postby dday9 » Mon Oct 14, 2013 7:39 pm

stranac wrote:Also, that function just looks wrong...
  • foo_customer should be provided as an argument to the function, instead of using a global to assign it.
  • Initializing an object should be the job of the __init__() method, not some random function
  • You're repeating yourself all over the place

And please use 4 spaces for indentation, 8 is too much.


Sorry I was using a combination of IDLE and Notepad++ and somehow it came out wit 8 spaces for the indentation. I fixed the duplicate entry issue, but I'm not sure I quite follow your list of suggestions. In response to the first suggestion, the reason why I don't pass foo_customer as an argument is because it's not needed. If the user decides to cancel out of the function(typing return) then foo_customer isn't needed so it's disposed of. I don't understand the second suggestion(I've only been programming with Python for about 3 days now). Finally in response to the third suggestion, how so? Is it the repetition of checking for if the user's entered return? If so, how should I change it?

By the way, here is the full source I have so far for the program. Like I said earlier, I've only started with Python a few days ago so if anything jumps out at you please let me know:

Code: Select all
import datetime

#The customer and policy class
class customer:
   name = ""
   dob = datetime.MINYEAR
   address = ""
   city = ""
   state = ""
   zip = 0
   policies = []
   
class policy:
   holder = customer
   number = 0
   location = ""
   city = ""
   state = ""
   zip = 0
   premium = 0
   effective = datetime.MINYEAR
   expiration = datetime.MAXYEAR


#These are the main menus
def main_menu():
   print ("Main - Menu")
   print ("___________")
   print ("Search -  1")
   print ("Add    -  2")
   print ("Remove -  3")
   print ()
   return int(input())

def search_menu():
   print ("Search   -    Menu")
   print ("__________________")
   print ("Last Name     -  1")
   print ("Policy Number -  2")
   print ("Main Menu     -  3")
   print ()
   return int(input())

def add_menu():
   print ("Add    -    Menu")
   print ("________________")
   print ("Add Customer - 1")
   print ("Add Policy   - 2")
   print ("Main Menu    - 3")
   return int(input())

def remove_menu():
   print ("Remove   -   Menu")
   print ("_________________")
   print ("Last Name     - 1")
   print ("Policy Number - 2")
   print ("Main Menu     - 3")
   return int(input())


#These are the sub menus

#Search Sub Menu(s)
def sub_search_name():
   #Search by last name
   print ("Please enter the last name. To return to the search menu, enter 'return'")
   name = input()
   if name == "return":
      return None
   else:
      return name

def sub_search_number():
   #Search by policy number
   print ("Please enter the policy number. To return to the search menu, enter 'return'")
   policy = input()   
   if policy == "return":
      return None
   else:
      return int(policy)

#Add Sub Menu(s)

def sub_add_customer():
   #Add Customer
   foo_customer = customer
   print ("If at any time you wish to exit without saving, type 'return'")
   print ("Please enter in the new customer's name:")
   
   name = input()
   if name == "return":
      return None
   else:
      foo_customer.name = name
         
   print ("Please enter in the new customer's date of birth:")
   
   dob = input()
   if dob == "return":
      return None
   else:
      foo_customer.dob = dob
         
   print ("Please enter in the new customer's address:")
   
   address = input()
   if address == "return":
      return None
   else:
      foo_customer.address = address
         
   print ("Please enter in the new customer's city:")
   
   city = input()
   if city == "return":
      return None
   else:
      foo_customer.city = city
         
   print ("Please enter in the new customer's state:")
   
   state = input()
   if state == "return":
      return None
   else:
      foo_customer.state = state
         
   print ("Please enter in the new customer's zip:")
   
   zip = input()
   if zip == "return":
      return None
   else:
      foo_customer.zip = int(zip)

   return foo_customer
   
def sub_add_policy():
   #Add Policy
   foo_policy = policy
   print ("If at any time you wish to exit without saving, type 'return'")
   print ("Please enter in the new policy number:")
   
   number = int(input())
   if number == "return":
      return None
   else:
      foo_policy.number = number
   
   print ("Please enter in the physical address:")
   
   location = input()
   if location == "return":
      return None
   else:
      foo_policy.location = location
      
   print ("Please enter in the city:")
   
   city = input()
   if city == "return":
      return None
   else:
      foo_policy.city = city
      
   print ("Please enter in the state:")
   
   state = input()
   if state == "return":
      return None
   else:
      foo_policy.state = state
      
   print ("Please enter in the zip code:")
   
   zip = input()
   if zip == "return":
      return None
   else:
      foo_policy.zip = zip
      
   print ("Please enter in the premium:")
   
   premium = int(input())
   if premium == "return":
      return None
   else:
      foo_policy.premium = premium
      
   print ("Please enter in the effective date:")
   
   effective = input()
   if effective == "return":
      return None
   else:
      foo_policy.effective = effective
      
   print ("Please enter in the expiration date:")
   
   expiration = input()
   if expiration == "return":
      return None
   else:
      foo_policy.expiration = expiration
      
   return foo_policy
   
#The main boolean should always be true until
#The program closes
main = True

#These booleans will determine which main menu will be shown
main_bool = False
search_bool = True
add_bool = True
remove_bool = True



while main == True:

   #Main Menu - Main
   if main_bool == False:
         
      while main_bool == False:
         main_int = main_menu()

         if main_int == 1:
            search_bool = False
            main_bool = True
         elif main_int == 2:
            add_bool = False
            main_bool = True
         elif main_int == 3:
            remove_bool = False
            main_bool = True
         else:
            print ("Invalid Input.")

   #Main Menu - Search
   elif search_bool == False:

      while search_bool == False:
         search_int = search_menu()

         if search_int == 1:
            #Sub Menu - Search By Last Name
            
            search_last = False
            while search_last == False:
               last_int = sub_search_name()
               search_last = True
                     
         elif search_int == 2:
            #Sub Menu - Search By Policy Number
                  
            search_policy = False
            while search_policy == False:
               policy_int = sub_search_number()
               search_policy = True
                     
         elif search_int == 3:
            main_bool = False
            search_bool = True
         else:
            print ("Invalid Input.")

   #Main Menu - Add
   elif add_bool == False:

      while add_bool == False:
         add_int = add_menu()

         if add_int == 1:
            #Sub Menu - Add Customer
         
            customer_bool = False
            while customer_bool == False:
               foo_customer = sub_add_customer()
               customer_bool = True
         elif add_int == 2:
            #Sub Menu - Add Policy
            
            policy_bool = False
            while policy_bool == False:
               foo_policy = sub_add_policy()
               policy_bool = True
         elif add_int == 3:
         
            #Return to the Main Menu - Main
            main_bool = False
            add_bool = True
         else:
            print ("Invalid Input.")

   #Main Menu - Remove
   elif remove_bool == False:
         
      while remove_bool == False:
         remove_int = remove_menu()

         if remove_int == 1:
            remove_bool = True
         elif remove_int == 2:
            remove_bool = True
         elif remove_int == 3:
            main_bool = False
            remove_bool = True
         else:
            print ("Invalid Input.")


It's something I've decided to take on, create a data management program using strictly Python and XML. XML is how I plan to store the customers/policies.


Edit - Wow, I really had to go fix those indentations it took me a while!
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Null Value

Postby stranac » Mon Oct 14, 2013 8:22 pm

I would suggest not using classes until you learn how to use them properly.
Any basic python tutorial should explain at least the basics.
There's even a tutorial on class basics here on the forum, written by ichabod: viewtopic.php?f=25&t=361

dday9 wrote:Finally in response to the third suggestion, how so? Is it the repetition of checking for if the user's entered return? If so, how should I change it?

One possibility is writing a function that does all of that for you.
Kinda like sub_search_name() and that other one, but more generic, so you can use it for all your inputs.

Also, all your menus could be created by a single function.

I haven't really payed close attention to the last 100-ish lines of that code, but I don't like how that looks.
There has to be a better/simpler way to do what you're doing there.

dday9 wrote:Edit - Wow, I really had to go fix those indentations it took me a while!

3 spaces is still weird, you should really use 4.
For other style hints, check PEP8.

Off topic: you a day[9] fan?
Friendship is magic!

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

Re: Null Value

Postby dday9 » Mon Oct 14, 2013 8:25 pm

Off topic: you a day[9] fan?

No idea what that is :P

My name is David Day and I was born '91. When I tried to make a user name a long time ago I tried making dday91 but it was taken, so I took dday9 and stuck with it pretty much ever since.
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Null Value

Postby stranac » Mon Oct 14, 2013 8:31 pm

Nvm then...
Friendship is magic!

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

Re: Null Value

Postby Kebap » Mon Oct 14, 2013 11:10 pm

This is just an idea on your sub_add_customer(), using python lists:
Code: Select all
questions = ["Please enter in the new customer's name:",
"Please enter in the new customer's date of birth:",
"Please enter in the new customer's address:"]
answers = []

for foo_question in questions:
  foo_answer = input(foo_question)
  if foo_answer in ["return", ""]:
    return
  answers.append(foo.answer)

foo_customer.name, foo_customer.dob, foo_customer.address = answers

This code does not grow as quickly when adding more questions. Also incorporating some other feedback
stranac wrote:Off topic: you a day[9] fan?
<3 yes
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Null Value

Postby dday9 » Mon Oct 14, 2013 11:58 pm

Oh wow, I would have never thought of that Kebap. +1 if there was a rep button.
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], snippsat and 5 guests