Python Forum
Decision statement problems - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Decision statement problems (/thread-16549.html)



Decision statement problems - erfanakbari1 - Mar-04-2019

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


RE: Decision statement problems - ichabod801 - Mar-04-2019

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.


RE: Decision statement problems - woooee - Mar-06-2019

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/