-2

I am trying to break out of a bad habit of using if/else too frequently. I am new to how functions work and the proper way to call them but I am constantly researching the correct way to implement them with my code. The code that I am making is suppose to check for 3 different words and if the word is not in the input then the user will receive a statement that says "rejected" if the word is correct it will say "accepted". The issue that I am facing is getting my program to work correctly. So far I have set up my program to check each index of the word and if it matches the full word it will be marked as accepted. I am trying to figure out the correct way to add a rejected flag and to avoid the error that I recieve after running this program.

def checker():
  q0 = input("enter word:")
  if (q0[0]) +(q0[1]) == "if":
    print ("accepted")
  if (q0[0]) + (q0[1]) + (q0[2]) + q0[3] == "else": 
    print("accepted")
  if(q0[0]) + (q0[1]) == "do":
    print("accepted")
  else:
    print("rejected")
checker()

For this program, I am not going to use a dictionary so I can correctly challenge myself and implement this in an automata fashion. How can I implement this code without getting the string index out of range error. I tried to put break after my print statement but it doesn't work.

Thanks in advance to everyone. This is my first post so if I have made any mistakes in my post please let me know!

4 Answers4

1

Here's an extensible one-liner for you:

def check():
    q = input().strip()
    acceptable = {'if', 'else', 'do'}
    print('accepted' if q in acceptable else 'rejected')

The variable acceptable is set; a data structure which is very quick to check if something is inside of it. You can modify this set (or pass it to check as an argument!) to change the range of acceptable words without changing the control flow of the program (as you would in your original if/else implementation that you're laudably trying to move away from).

EDIT: I guess it's not strictly a 'one-liner'...

wbadart
  • 2,583
  • 1
  • 13
  • 27
0

First, why do you access each character of the input string, then concatenate them again, then compare to a target string("if", "else", "do")?

Second, why do you use if statements repeatedly if matching either one of them will lead to the same result (print("accepted")?

Try this:

def checker():
  q0 = input("enter word:")
  if q0 in ["if", "else", "do"]:
    print("accepted")
  else:
    print("rejected")
checker()

Now, you just compare a string q0 to another (each element of the list ["if", "else", "do"]). Also, the first hit in the list will make stop comparing anymore and will continue on to print "accepted".

++ Just to let you know why are you seeing "index out of range error", you are accessing each character of q0 without knowing how many there are. So if the user inputs a string like a, there's no q0[1] or q0[2], but you're asking your program to access it. Thus index out of range error.

Ignatius
  • 2,745
  • 2
  • 20
  • 32
  • Thanks man. It sucks because the class I’m doing this for doesn’t want me to use an array or dictionary of any kind. I would set it up just like this if possible. I ended up creating a program that accepts input letter by letter and it will say if it’s correct or not. I wanted to try and create a program that takes the whole word but at this point it just looks out of my reach without a dictionary/array. – DonnieTheNewb Feb 20 '19 at 05:49
  • If you really want not to use a list (there is no array in python), you can keep your program but change each condition of `if` statements read something like `q0 == "if"`, `q0 == "else"` and so on. Also, consider using `if-elif-elif-else` sequence instead of `if-if-if-else` – Ignatius Feb 20 '19 at 05:53
0

You can do this with a for loop and one if statement if that is better for you. Simply put all the accepted values into a list and check if each word is in q0.

def checker():
    q0 = input('enter word:')
    for i in ['if', 'else', 'do']:
        result = ('accepted' if i in q0 else 'rejected')
        if result == 'accepted':
            break
    print(result)
Joe Guida
  • 29
  • 5
0

you can do it as one liner with lambda function.

checker = lambda q: print("accepted" if q in ["elif", "if", "else"] else "rejected")
checker()

here is a sample

>>> checker = lambda q: print("accepted" if q in ["elif", "if", "else"] else 
"rejected")
>>> checker("if")
accepted
>>> checker("fool")
rejected
Akshay Nailwal
  • 196
  • 1
  • 9
  • Note that assigning lambda function to an identifier is considered harmful by [PEP8](https://www.python.org/dev/peps/pep-0008/#programming-recommendations). – Ignatius Feb 21 '19 at 07:43
  • Thanks, I know that lambda function assignment to a identifier is not recommended but I was just giving an example that it can be done this way. – Akshay Nailwal Feb 21 '19 at 08:55