Python Forum

Full Version: Decision statement problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys , I'm trying to solve some exercises about digits and string and whole decision statement and I don't know actually how to check whether a input that is been taken by user consists digits or strings or both!
inptUser = input('Please enter a variable : ')
print(inptUser)

for x in inptUser:
  if x.isdigit():
    print(' The input is full of digits ')
This is my code but I don't know what's wrong with it . I appreciate if you can help me with this .
Thank you Smile
The value of inptUser is a string. Looping over a string, as you do on line 4, loops over each character in the string. So you are checking each digit of the string, and for each one that is a digit, you are printing that the input is all digits. Note that isdigit will check to see if a string is all digits: it will check each character by itself. However, it would return False if you only have some digits, like 'ichabod801', so it's not good for checking if there are any digits in the string.

It's not clear to me exactly what you supposed to do for this exercise, so I'm not sure how I would correct your code.
You want to do something when it is not a digit
def is_it_digit(inptUser):
    for x in inptUser:
        if not x.isdigit():
            print('a not digit found')
            return False
    print(' The input is full of digits ')
    return True

inptUser = input('Please enter a variable : ')
print(inptUser)
print(is_it_digit(inptUser)) 
And when you have some time, take a look at the Python Style Guide; variables are lower_case_with_underscores
https://www.python.org/dev/peps/pep-0008/