1

I'm working on a very simple problem, but I had some problems.

What I want to write is a function (using the for loop) that veifies if the string put in input is composed by the following letters: a, b, c, and d. If the string contains a letter different from a,b,c or d, the program should give an error message. If the string contains just 1,2, or 3 of the 4 mentioned letters, it will be correct.

  • The string 'abbbbbcd' will be correct.
  • The string 'ab' will be correct.
  • The string 'ghjja' will be Incorrect.
  • The sting 'aaabbbcccff' will be Incorrect.

This is my current version of the code:

def string(f):
    for i in range(len(f)):
        if f[i]!='a' or f[i]!='b' or f[i]!='c' or f[i]!=d':
            print('error')
        else:
            print('ok')
3
  • 1
    And what is your current version of the code to do that? Commented Jan 20, 2020 at 21:03
  • 1
    Welcome to stack overflow! Unfortunately this is not a code-writing or tutorial site. Please provide a minimal reproducible example for your issue including code for what you've tried and any errors you're receiving, or a description of the problem you're experiencing Commented Jan 20, 2020 at 21:04
  • i posted it @B.Go Commented Jan 20, 2020 at 21:07

4 Answers 4

4

The problem with your code is:

  • you're using or instead of and, its impossible for each character to equal all 4 inputs
  • You're missing a starting quote for d
  • you also print the output on every iteration of the loop

you'd be better off turning this into a function.

def only_abcd(f):
    for i in range(len(f)):
        if f[i]!='a' and f[i]!='b' and f[i]!='c' and f[i]!='d':
            return False
    return True

print("ok" if only_abcd(f) else "error")

You could also use all and in

if not all(i in "abcd" for i in f):
    print("error")
else:
    print("ok")
Sign up to request clarification or add additional context in comments.

2 Comments

thank you! can you please explain me, why if i modify yourcode like that, it does not work anymore? ` for i in range(len(f)): if f[i]!='a' and f[i]!='c' and f[i]!='b' and f[i]!='d': return False else: return True` (I modified the position of 'return True')
@marry Jones- because that would only be checking the first character, not the entire string, the difference here is it will only return true if the for loop completes without finding an invalid character
1

You can use that code:

for i in string:
    if i not in ['a', 'b', 'c', 'd']:
        raise Exception('String contains wrong letter')

Comments

0

Solution:

  1. Use and instead of or.
  2. Add the missing starting quote for d.
  3. Use a flag variable for tracking the error.
  4. Don't put else block for the if condition inside for loop.

After following above instructions your code will be like this:

def string(f):
    error=False
    for i in range(len(f)):
        if f[i]!='a' and f[i]!='b' and f[i]!='c' and f[i]!='d':
            print('error')
            error=True
            break
    else:
        print('ok')

Alternatively you can try this:

def string(f):
    letters = ['a','b','c','d']
    error=False
    for i in f:
        if i not in letters:
            error=True
            print('error')
            break
    else:
        print('ok')

Comments

0

A compact one liner:

def abcd_test(input):
    return False not in [x in "abcd" for x in input]

Comments

Your Answer

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