Python Forum
Decision statement problems
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decision statement problems
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using ID3 Estimator for Decision Trees student6306 2 1,366 Jun-15-2023, 04:11 PM
Last Post: student6306
  What's the full answer to this question in python? (bayesian decision theory) Hblaugrana 1 2,464 Oct-31-2018, 02:22 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020