0

I'm trying to use raw_input to get input from the user, i want to catch the instances where the user may enter a number. I've been searching threw stack overflow and various other websites and i haven't come across anything that made much sense to me. Since raw_input always returns a string is it even possible to catch such an instance?

class Employee(object):

    def __init__(self,name,pay_rate,hours):

        self.name = name
        self.pay_rate = pay_rate
        self.hours = ("mon","tues","wed","thursday","friday","saturday","sunday")

    def __str__(self):
        return self.name       

    @property
    def weekly_total(self):
        return sum(self.hours)

    @classmethod
    def from_input(cls):
        while True:
            try:
                name = raw_input("Enter new employee name\n")
            except ValueError:
                print("\n  This is a integer  ")
                continue           
            else:
                break
        while True:
            try:
                pay = input("Enter pay rate  ")
            except ValueError:
                print("You must enter a value  ")
                continue
            else:
                break
        while True:
            try:
                hours = input("Enter a tuple for days monday through sunday  ")
            except ValueError:
                print("use must enter a tuple with 7 integer values")
                continue
            else:
                break
        return cls(name,pay,hours)    

employee = Employee.from_input()
print str(employee)
2
  • 4
    Do you mean you want to ensure the user enters a number when you want a number, or vice versa? Note that you shouldn't be using input() - int(raw_input(...)) is more explicit. Commented Jul 28, 2014 at 16:36
  • Vice-versa but i will need the former as well for the pay input and hours tuple input. I want to ensure a string is entered for the name and then ensure that float values are entered for pay and hours. Commented Jul 28, 2014 at 16:39

1 Answer 1

1

I would split this into separate functions; there are neater ways to do this, but I have made them as similar as possible so you can see the process:

def get_int_input(prompt):
    while True:
        s = raw_input(prompt)
        try:
            i = int(s)
        except ValueError:
            print "Please enter an integer."
        else:
            return i

def get_non_int_input(prompt):
    while True:
        s = raw_input(prompt)
        try:
            i = int(s)
        except ValueError:
            return s
        else:
            print "Please don't enter an integer."
Sign up to request clarification or add additional context in comments.

8 Comments

Oh, i was trying things similar to this i was on the right track, i see exactly what you did here thanks again.
So i can just code two similar functions and then call these functions inside my from_input method to do the checking?
Yes, exactly. This cuts down on duplication.
I'm confused about what to do with the name , pay and hours inputs in the from_input method now that i have these two new functions do i need to pass each one of these into my new check functions in order to keep my final return statement cls(name,pay,hours) working properly?
It's not clear what you're asking. from_input should just contain e.g. pay = get_int_input("Enter pay rate: "), and a for loop for the daily hours, then return cls(...) at the end. The sub-functions are called to get the input, not just to check it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.