Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
you need to find only numbers which do not contain 9.
input:
Hi5 Welcome567 batch 2019.

output:
5 567

2019 should not be printed because it has 9 in it.
If a string has no number in it print -1.

(P.S: I need to find it without using import functions).

What I have tried:

Python
a = input().split()
n = '9'
res = ''
for i in a:
    if i.isdigit():
        res = i
        if i not in n:
            print(str(res))
        else: 
            break
    if n in a:
        print(-1)
Posted
Updated 19-Feb-22 23:13pm

You should not use string().split(), as that assumes that all numbers are complete substrings. The firs string after the split will be "Hi5" which does not begin with a digit.
You should check all characters on by one, accumulating digits. As soon as you find a non-digit character then you should check the currently accumulated string to see if it contains a 9 character. So starting with a being the complete string your loop should be:
Python
for i in a:
    if i.isdigit():
        res += i # add this digit to the current string
    else:
    #
    # When you find a non-digit you need to take action to decide whether
    # the current string needs to be displayed or ignored. I leave this as
    # an exercise for you.
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Start by thinking about how you would do it manually: you'd find the start of a number, then process each digit until you found the end, then go back to looking for the start of a number. When you got to the end, you'd decide whether to keep the number of throw it away if it held a nine. So think about that and how you might automate it.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900