0
employee = float(raw_input('Employee code number or 0 for guest:') or 0.0)

if employee == isalpha:
    print "Nice try buddy"
    print "Welcome BIG_OLD_BUDDY" 

This code does not recognize alphabetical input.

too honest for this site
  • 11,417
  • 3
  • 27
  • 49
GAVDADDY
  • 21
  • 3
  • That's because you're immediately parsing it as a float. Don't cast a raw value, make it *can* be cast first, probably with a try:except block – Sterling Archer Aug 09 '17 at 03:25
  • `employee = raw_input('Employee code number or 0 for guest:')`;`if employee.isdigit(): employee = int(employee) else: something else...` – Alexander Aug 09 '17 at 03:31

4 Answers4

1

There are 2 ways.

  1. You can catch the the exceptions and pass.
try:
    employee = float(raw_input('Employee code number or 0 for guest: ') or 0.0)
except KnownException:
    #  You can handle Known exception here.
    pass
except Exception, e:
    # notify user
    print str(e)
  1. Check for the type of input and then do what you want to do.
employee = raw_input('Employee code number or 0 for guest:')

if(employee.isalpha()):
    print "Nice try buddy"
    print "Welcome BIG_OLD_BUDDY"
else:
    print "Your employee no is:" + str(employee)

Do not use try and catch until and unless there are chances of unknown exceptions. To handle things with if and else are recommended.

Read more about : why not to use exceptions as regular flow of control

yt.
  • 31
  • 7
Ujjaval Moradiya
  • 212
  • 1
  • 12
0

Use a try

try:
    employee = float(raw_input('Employee code number or 0 for guest: ') or 0.0)
except ValueError:
    print "Nice try buddy"
    print "Welcome BIG_OLD_BUDDY"
SomeGuyOnAComputer
  • 3,408
  • 3
  • 27
  • 52
0

You can use try/except as the other answer suggested, and if you want to use str.isalpha() you have to call it on the string, not compare it with the string. For example:

employee = raw_input('Employee code number or 0 for guest:')

if employee.isalpha():
    print "Nice try buddy"
    print "Welcome BIG_OLD_BUDDY" 
else:
    employee = float(employee)
Mohd
  • 5,075
  • 7
  • 17
  • 29
0

If you want it to only accept positive integers you can check usingisdigit() which is basically the opposite of isalpha().Try:

    employee = raw_input('Employee code number or 0 for guest:')

    if employee.isdigit()==False:
      print "Nice try buddy"
      print "Welcome BIG_OLD_BUDDY"
    elif int(employee)==0:
      print "You are a guest"
    else:
      print "Your employee no is:" ,employee

I also added a bit of code using elif to check if you're a guest

yt.
  • 31
  • 7