• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I make a sentinel value not be initialized in a class/method?

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am testing out some basic Object Oriented Programming in Python.  The basics:

User enters a name
While loop with a sentinel value of "quit" will continue entering names until the sentinel value is reached
The object is created with the inputted value (NAME and until a sentinel value is entered)
the object is then printed out using the constructor __str__
in my solution ( I am saying this because someone might have a work around that doesn't include the following below)
I want to use __init__
I want to use __str__
I want to use a while loop with a sentinel value

EXPLANATION:
I want to have the user enter in their NAME using a while loop, with "quit" being the sentinel value that breaks the loop.  Once the name is entered, an object is created and it is passed into a constructor def __ini__ (self, name) and then I have them return a string value using __str__ for the name entered.
The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out.  How can I get around this?  I hope this makes sense.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us what have you done so far.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patrick De wrote:The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out.  How can I get around this?

Why you passing QUIT to the constructor in the first place? Can't you check if value the is QUIT, in which case you don't pass argument to the constructor (of X (maybe Person) object) and don't create object.

an example pseudo code how I imagine you might have as for now
 
Patrick De
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking about something like this :

While quit is not entered
enter your name (quit to exit)
enter your age (quit to exit)
enter your id (quit to exit)


All of this information gets passed into the class via __ini__
The initial entering of information isn't in a class
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Please show us what have you done so far.

 
Patrick De
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will post the code when I get it closer to being somewhat correct.  I find for myself, working this way is more productive, I hope people don't mind.  I have put more descriptive pseudo-code below :
Ok, I changed the sentinel value and I am not going to be using that anymore.  Instead I am going to ask the user if they want to keep adding to the database, if no, then break, if yes, then continue.  

Another problem!  

can't assign to a function call


count= 0

class car
initialize all of the values and put self so they are available to the entire class
def __init__(self, model...)
 self.model=model etc
 self.serial = serial
 self.doors=doors



while loop
input model =what is the model
input serial = what is the serial
input doors = how many doors
count = count + 1
#create the instance/object
mycar (count) = car(model, serial, doors)

input do you want another car in the database?
if yes
 continue
if no
 break

The issue is that when creating an object/instance, python won't let me use this syntax of having -- mycar(count) which would give me multiple objects (mycar1, mycar2, mycar3 etc) with the count variable.  I am not sure why.
 
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic