0

I don't know what's wrong in this code. I want this program to print the character or to show a message, for example "you didn't type only 1 character". Can you help me please ?

def PrintVariable(lit):
variable = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
            "n", "o", "p", "q", "r", "s", "t", "u", "w", "x", "y", "z"]
for i in variable:
    if(i == lit):
        print("Your character is ", i)

def CheckIfLitIsNumber(lit):
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for z in numbers:
        if (z == lit):
           return True
        else:
           return False

lit = (input("Give a character "))
if len(lit) == 1 and (CheckIfLitIsNumber(lit) is False):
    PrintVariable(lit)
else:
    print("You didn't give a character or you've entered a word")
Andrei
  • 25
  • 5
  • What is the actual output? – user3272686 Feb 08 '17 at 23:53
  • 1
    Both your `for`s are not necessary - `if lit in variable:` will suffice. Furthermore, `lit.isalpha()` and `lit.isdigit()` are standard library functions so you don't need the above. – Ken Y-N Feb 08 '17 at 23:54
  • Your question is phrased wrong, It looks like your code's error is when it is a number, not a word. Are you sure it doesn't give the right message when you typed multiple characters? – UnsignedByte Feb 09 '17 at 00:31

2 Answers2

1

With the first iteration of for loop in CheckIfLitIsNumber, if z is not equal to lit; your code is returning False. In other words, in your current code for is having no meaning as it is returning the True/False based on the comparison of numbers[0]==lit.

Instead you should be returning the False value at the completion of for loop. Hence, you code should be like:

def CheckIfLitIsNumber(lit):
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for z in numbers:
        if (z == lit):
           return True
    else:
        return False
    # ^   ^  please note the indentation    

Also note that the input() in Python always returns str. For changing it to int type, you'll have to type-cast it. Hence, before passing the value to CheckIfLitIsNumber, only then you'll be able to perform integer comparison. Please refer: How can I read inputs as integers in Python?


However, you do not need both of these functions. You may get the same result via just doing:

import string
variable = string.lowercase  # string of all the lowercase characters

lit = input("Give a character ")

#                          v check presence of `lit` in `variable` string
if len(lit) == 1  and lit in variable:  
    print("Your character is ", lit)
else:
    print("You didn't give a character or you've entered a word")

It is even better to change your if condition to:

#                         v checks whether number is alphabet or not
if len(lit) == 1  and lit.isalpha():
Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • 1
    Also, if he wants to use the CheckifLitIsNumber and compare to a list of integers, he needs to `CheckIfLitIsNumber(int(lit))`. `input()` returns a string in Python3. – TemporalWolf Feb 09 '17 at 00:07
  • 2
    `not lit.isdigit()` should suffice as you'd need a `try/except` statement for the case when it is a number. – Steven Summers Feb 09 '17 at 00:15
  • Yes. I missed that. @Andrei please see the comment mentioned by TemporalWolf and Steven. For details, you may refer: [How can I read inputs as integers in Python?](http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python) – Moinuddin Quadri Feb 09 '17 at 00:17
0

You have an error when doing

def CheckIfLitIsNumber(lit):
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for z in numbers:
        if (z == lit):
           return True
    else:
        return False

Since numbers is an int array, comparing that to lit, a string, will always return false. For example:

>>> "1" == 1
False

Instead, what you would want to do is catch a TypeError, like so:

Do NOT use this code, look below to see what to use instead.

def CheckIfLitIsNumber(lit):
    try:
        return isinstance(int(lit), int)
    Except TypeError:
        return false

In this example, if lit is actually a string, running int(lit) will give a TypeError, which is subsequently caught and return false is run instead. Though this would work, it would be much simpler just to use lit.isdigit(), like this:

def CheckIfLitIsNumber(lit):
    return not lit.isdigit()
UnsignedByte
  • 849
  • 10
  • 29