-1

So I am trying to write a small script in python that will differentiate inputs, and when the appropriate input is given, an int, it will give the correlating answer. I know there are a number of posts on this topic that are similar but I was having trouble finding my answer. Here is what I have.

oktas = input("Oktas: ")

def sky_condition(oktas):
   if isinstance(oktas, int) == True:
      if oktas == 0:
         print ("CLR")
      elif oktas == 1 or oktas == 2:
         print ("FEW")
      elif oktas == 3 or oktas == 4:
         print ("SCT")
      elif oktas >= 5 and oktas <= 7:
         print ("BKN")
      elif oktas == 8:
         print ("OVC")
      elif oktas < 0 or oktas > 8:
         raise ValueError("Oktas provided are above or below the allowed limits.")
   elif isinstance(oktas, int) == False:
      raise NameError("You suck at following instructions.")

skycondition(oktas)

When you put in an int or a str(as in something in "quotation marks") then it processes everything as it should. However if I just put a letter or word without quotation marks, for example h, it raises the error

NameError: name 'h' is not defined 

It produces the error I want but not how I wanted it. How can I code it so it will raise the error how I want it?

Samuel Smith
  • 5
  • 1
  • 4
  • 1
    Is this Python 2? If so, you should consider using `raw_input` instead of `input`, and then attempting to convert to an integer using `int(oktas)`. (also, please add the `python-2.x` tag, because this is one area where the behaviour is different to Python 3) – Aurora0001 Oct 11 '16 at 19:13
  • `isinstance` already returns a Boolean value; there is no need to check if it equals another Boolean value to generate a *third* Boolean value. – chepner Oct 11 '16 at 19:18
  • See the nuances of `input` (which apears to be your problem) in this other [stackoverflow answer](http://stackoverflow.com/a/15129556/5847976) – jadsq Oct 11 '16 at 19:18

1 Answers1

4

Several things wrong with your code, I will try to point them out, fix them and explain to you what you are doing wrong.

oktas = input("Oktas: ") #returns str only if you are running Python 3.x+. 
                         #returns int if it's an int for Python 2.x
                         #not sure which version of python you are running. 

def sky_condition(oktas):
   if isinstance(oktas, int) == True:
      if oktas == 0:
         print ("CLR")
      elif oktas == 1 or oktas == 2:
         print ("FEW")
      elif oktas == 3 or oktas == 4:
         print ("SCT")
      elif oktas >= 5 and oktas <= 7:
         print ("BKN")
      elif oktas == 8:
         print ("OVC")
      elif oktas < 0 or oktas > 8:
         raise ValueError("Oktas provided are above or below the allowed limits.")
   elif isinstance(oktas, int) == False:
      raise NameError("You suck at following instructions.")

sky_condition(oktas) # you had skycondition(oktas) which wasn't your function name.

Overall this program works, just typos on your end. If you are running Python 3, you will have to do some error checking for the input oktas since like I said, Python 3.x returns a str for input(). You will need to cast the input with int(oktas) but then that would basically defeat the purpose of your isinstance() check since it will be an int. A good way of working around this is:

if oktas.isdigit():
     oktas = int(oktas)
     #rest of your code

The isdigit() function returns true if a str contains digits 0-9 only, then you can cast your oktas to int.

Like the comment says if you are using Python 2.x you should avoid using input() and use raw_input(). A few reasons:

raw_input() basically it will return a str object no matter what you give. If you give it 5 it will give you a str object: "5" If you give it "5" it will give you a str object: "5"

input() this is where your problem is. In Python 2.x input is basically doing eval(input()). You can read up on eval here. Basically it tries to evaluate the given expression. Since numbers and strings are expressions themselves, it will try to run your h input as literally h instead of a str. Once again use raw_input() to avoid this in Python 2.x.

I'm assuming you are using Python 2.x or you have other codes you aren't showing. Use raw_input() and cast your input accordingly.

MooingRawr
  • 4,901
  • 3
  • 24
  • 31
  • 2
    This still doesn't address specifically why inputs such as `h` do not work, but `"h"` does. You should explain why that doesn't work to help the OP understand more (perhaps mention that Python 2's `input` uses `eval`?) – Aurora0001 Oct 11 '16 at 19:16
  • 1
    Fair enough I will edited it. – MooingRawr Oct 11 '16 at 19:18