-1

I am trying to define a simple function which checks if what the user placed into the arguments equal the letter B (No reason. Just made it B.) This is my code:

def letter_check_B(letter):
    letter = str(letter)
    if letter == 'B':
        print("You entered B")
    else:
        print("You did not enter B")

However, when using the above code, by typing in:

 letter_check_B(B)

I get an error stating:

NameError: name 'B' is not defined

How would I go about to fix this without changing how the overall code works. I want to be able to make this code work by placing the letter into the argument (if that is possible).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Yuxie
  • 137
  • 1
  • 10

2 Answers2

0

You can't use a plain B as a value in Python. The syntax will always see that as a variable reference. Since you haven't ever assigned anything to the name B, you get a NameError exception.

The str() function can only work on values; it'll work on an integer for example:

>>> str(1)
'1'

You could create a string value with a string literal expression:

>>> 'B'
'B'

Passing that to the str() function is a no-op (the same string value is returned again):

>>> str('B')
'B'

but it'd make your function work nonetheless:

>>> letter_check_B('B')
You entered B

Note that any method you'd use to get user input, would return a value anyway. The input() function for example, returns an object (value) of type str:

>>> input('Enter a letter: ')
Enter a letter: B
'B'
>>> type(_)  # what is the type of the value produced by the last expression?
<class 'str'>

You may want to read up on getting and validating user input, see Asking the user for input until they give a valid response

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Okay. Thanks for your answer. However, due to my actual code allowing both the use of letters and numbers to be inputted (which does different things), is there a way for a letter to be placed in without the speech marks? Also I do not want to use a global variable and assign the user input to that variable. – Yuxie Apr 07 '16 at 07:33
  • @Yuxie: the *user* of your program doesn't use quotes to enter a letter. They are not writing code. Look closely at my use of `input()`; there are no quotes around the `B` entered. – Martijn Pieters Apr 07 '16 at 07:39
  • @Yuxie: instead, your code will have to see if what was entered could possibly be a number. See [How do I check if a string is a number (float) in Python?](http://stackoverflow.com/q/354038) and [Parse String to Float or Int](http://stackoverflow.com/q/379906). – Martijn Pieters Apr 07 '16 at 07:40
  • Alright. So i'm taking there are no way to do everything I am trying to do in just one definition, and that I must first ask for the input before using it in my defined function. And yes. I have already worked on a code which utilises ''.isdigit() and int() to parse a string to an int. Thanks for your answer. – Yuxie Apr 07 '16 at 07:44
  • @Yuxie: That depends on what you are trying to do. Of course you can pass any value to your function as long as the conversion to a string will work. – Matthias Apr 07 '16 at 08:40
0

B is a name (the Python word for a variable); and 'B' is a string with one character.

Since you didn't define B, Python is telling you that you are trying to pass a name that isn't defined.

Note the quotes 'B' make a difference here. You need to pass a string, like this:

letter_check_B('B')

You should read up on the string type.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • thanks for the answer. Is there a way to just allow the user to type in B without the ' '? Maybe using input()? – Yuxie Apr 07 '16 at 07:15
  • Just use input, like this: `user_letter = input('Please enter a letter: ')`, and the user can type `B` (without quotes); and it will be stored in `user_letter` as a string. – Burhan Khalid Apr 07 '16 at 07:16
  • okay. Thanks for the answer. I'll go about to try implement that instead. – Yuxie Apr 07 '16 at 07:18