Bottom Page

Thread Rating:
• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Decision statement problems erfanakbari1 Silly Frenchman Posts: 20 Threads: 14 Joined: Sep 2018 Reputation: 0 Likes received: 0 #1 Mar-04-2019, 07:10 PM (This post was last modified: Mar-04-2019, 08:21 PM by erfanakbari1. Edited 1 time in total.) 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 ichabod801 Number Four Posts: 4,103 Threads: 90 Joined: Sep 2016 Reputation: 254 Likes received: 1224 #2 Mar-04-2019, 08:44 PM 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 woooee Minister of Silly Walks Posts: 453 Threads: 0 Joined: Feb 2018 Reputation: 33 Likes received: 36 #3 Mar-06-2019, 12:27 AM (This post was last modified: Mar-06-2019, 12:28 AM by woooee. Edited 1 time in total.) 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/ « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post What's the full answer to this question in python? (bayesian decision theory) Hblaugrana 1 446 Oct-31-2018, 02:22 PM Last Post: j.crater

Forum Jump:

Users browsing this thread: 1 Guest(s)