-1

I'm having trouble trying to execute this code, I want the user to input a value, the program checks if that value is a string then it returns the length. If the value contains whitespaces the programs remove the whitespace and print the length. But if it contains any integer values the program returns "No Integers is Allowed"

This is the code:

def length(Name):
    long = len(Name)
    return long

new_length = input("Please Enter Your name You can use Spaces: ")
value1 = new_length
if value1.isspace() == True:
  print("This is Before Removing Spaces: " + value1)
  value2 = value1.replace(" ", "")
  print("This is After Removing Spaces: " + value2)
elif value1.isalpha() == True: 
  print("Here is The Length: ", length(value1))
elif value1.isdigit() == True:
    print("Integers are not allowed! ")
else:
    print("There's someting wrong with  "+ value1)

So if you can help me with that I appreciate it. Thanks

Imran Abdalla
  • 115
  • 1
  • 10
  • _So if you can help me with that I appreciate it._ Help you with what, what is the issue? – AMC Mar 24 '20 at 02:23

4 Answers4

1

I don't think the str.isspace, str.isalpha and str.isdigit methods do what you expect them to do. To start with, they all test if all the characters in the string you enter are of the type that is described in their name. Your code seems to be expecting them to be return True if any of the characters match. That is, if there are any spaces, you want to remove them and show the two lengths, before and after.

There's no single string method that will do that test for you in Python. You could use regular expressions (which are more powerful, but much more complicated), or you could write some slightly more elaborate code to do the test. I'd suggest using the any function, and passing it a generator expression that calls the method you want on each character in the string.

if any(c.isspace() for c in user_str):
    ...

This may not be exactly what you want for all of your tests. The desired logic of your code is not entirely obvious, as there are a number of corner cases that your output doesn't specifically address. Is a string that contains both letters and numbers valid? How about one that has spaces in between numbers, but no letters at all? You may need to reorder the conditions of your if/elif/else statements so that they match what you intend.

I'd also note that the variable name you used for user input, new_length, is very misleading. It's not a length, its the string you want to measure the length of! It's a lot easier to make logic errors about variables that have misleading or unclear variable names, so taking time to go back and reconsider names you chose earlier is sometimes a good idea, as it can improve the clarity of your code a lot! Descriptive variable names are good, but it's a tradeoff between clarity and brevity, as long names are tedious to type (and prone to typos). They also can lead to line length issues, which can make it less convenient to see all your code on your editor screen at once.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

You can use this function to check if the input string contains a number:

def hasNumbers(inputString):
        return any(char.isdigit() for char in inputString)

It returns true if there is a number and false if there is not.

As for the whitespaces you can ommit isspace(). Using replace() alone will do the job, even if there are no whitespaces.

stri='jshsb sjhsvs jwjjs'
stri=stri.replace(' ','')
Charalamm
  • 1,547
  • 1
  • 11
  • 27
0

I suggest reading the documentation in these cases. For isspace, note here that

Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.

That is, if there's anything that's not a space there, it will be False. Which is annoying, but why check in the first place? Just do the replacement! If there's no whitespace, it won't do nothing. If you need to print those statements, you can do

if ' ' in value1:
  ...

(of course, this doesn't consider all the possible kinds of whitespaces, check the other answers for doing the for loop if you need that)

Next, I believe you need to remove the elifs and just use if statements, since note that if you input a name with a space, it will print the name with the spaces removed... and nothing after that. Not even if it has integers in it. This is because elif statements don't execute once another above them did.

There are many other things you need to consider, but these two I think you should consider first. Hope it's useful!

Kevin Languasco
  • 2,318
  • 1
  • 14
  • 20
-1

You can use the re module in Python to check for white spaces in your string. It returns True if there are white spaces and False otherwise.

import re
def length(Name):
    long = len(Name)
    return long

new_length = input("Please Enter Your name You can use Spaces: ")
value1 = new_length
if re.search('\s', value1):
  print("This is Before Removing Spaces: " + value1)
  value2 = value1.replace(" ", "")
  print("This is After Removing Spaces: " + value2)
  print("Here is The Length: ", length(value2))
elif value1.isalpha() == True: 
  print("Here is The Length: ", length(value1))
elif value1.isdigit() == True:
    print("Integers are not allowed! ")
else:
    print("There's someting wrong with  "+ value1)
abigya
  • 42
  • 6