How can I fix this cod?

Question:

x = str(input("type a letter : "))

if x == x.islower:
    print ("capital")
else:
    print("small")
    

This is my code I wanna tell a letter is capital or small but Idk what’s the problem

Asked By: Mehdi

||

Answers:

There are some basic logical and syntactic mistakes in your code .This would fix your code:

x = str(input("type a letter : "))

if x.islower():
    print ("small")
else:
    print("capital")
Answered By: LearnerMantis

The str.islower() is a function that returns a boolean.

from the docs:

Return True if all cased characters [4] in the string are lowercase and there is at least one cased character, False otherwise.

What’s the problem?

str.islower is a function (method) and you are comparing it to a str type, that are surely never gonna be equal.

for example, if i have this function:

def func():...

That function is never gonna be equal to a Literal value, it can be compared only with another function.

Advices

Here are some suggestions for your code:

  1. check printing the value and make sure that are matching types when you compare it.
  2. In the first line (e.g str(input(...))) the str call is redundant because the input function also returns a string.
  3. Check using python tutor if you don’t find an issue with your code

What should you use?

in your case there are various ways you can accomplish this simple task, but the one that is probably more coherent to what you planned for is using str.lower() and not the str.islower() method.

Code using this idea:

ch = input("character: ")[0] #take just the first character inputted

if ch.lower() == ch:
    print("small")
else:
    print("capital")
Answered By: XxJames07-
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.