0

I'm very new to Python, please help.

I can only use if else statements, no loops or anything special like that.

This program asks the user for one string. It counts the number of different digits in the string. for example a11111a1a1 contains a single digit, while a1s2d1d2d1d2d1 contains 2 digits.

Examples:

% python3 countNumbers.py
enter string: qwerty
there are no digits in string: "qwerty"

% python3 countNumbers.py
enter string: asdf1sdfg
there is one digit in string: "asdf1sdfg"

% python3 countNumbers.py
enter string: as1as2as333333
there are 3 different digits in string: "as1as2as333333"

% python3 countNumbers.py
enter string: a0123456789x
there are 10 different digits in string: "a0123456789x"
num = input("enter string: ")
count = 0

if "0" in num:
    count = count + 1
if "1" in num:
    count = count + 1
if "2" in num:
    count = count + 1
if "3" in num:
    count = count + 1
if "4" in num:
    count = count + 1
if "5" in num:
    count = count + 1
if "6" in num:
    count = count + 1
if "7" in num:
    count = count + 1
if "8" in num:
    count = count + 1
if "9" in num:
    count = count + 1

if count >= 1:
    print("there is one digit in string: ", num)
elif count >= 2:
    print("there are two digits in string: ", num)
else:
    print("there are no digits in string: ", num)

1
  • 2
    If you think "loops" are "special" I doubt you will find any of the easiest answers we can provide applicable. Commented Feb 20, 2020 at 7:50

6 Answers 6

2

This is using regex, so import the regex library:

import re

Use the following Python to find individual digits:

a = re.findall('\d', string)

If you want more than one digit together use:

a = re.findall('\d+', string)

Then use the following to count (len) the set (which makes it unique):

len(set(a))
Sign up to request clarification or add additional context in comments.

1 Comment

OK, as per new edit this answer is redundant but may be useful for others...
1

You can do it this way:

len(set(a).intersection(map(str, range(10))))

Comments

1

One liner without re, using plain list and str operations

x='a1s2d1d2d1d2d1'
>>> sum( i.isdigit() for i in list(set(list(x))) )
2
>>>

Breaking it down: convert string to list and then to set and then back to list, will remove duplicates and then count the digits.

>>>
>>> l = list(set(list(x)))
>>> sum( i.isdigit() for i in l )
2

Note: this uses loop inside one liner.

Comments

0

Well if you knew loop it would have been much much easier and would have looked pythonic. However, I have modified your code here and here is the solution using 'if-else' statement and 'sets'

s = set()
if "0" in num:
    s.add(0)
if "1" in num:
    s.add(1)
if "2" in num:
    s.add(2)
if "3" in num:
    s.add(3)
if "4" in num:
    s.add(4)
if "5" in num:
    s.add(5)
if "6" in num:
    s.add(6)
if "7" in num:
    s.add(7)
if "8" in num:
    s.add(8)
if "9" in num:
    s.add(9)
print(len(s))

Comments

-1
num=input()
count=0

for i in num:
    if i in '0123456789':
        count+=1

print(count)

You can use "in" in a single line

Comments

-1

Here it is

count_dict = {}
for i in range(0, 10):
    if i in num and i not in count_dict:
        count_dict[i] = 1 # just adding the integer to a dict
    else:
        continue


print('there are {} different integers in string {}'.format(len(count_dict), num)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.