Not Breaking and won't repeat the Entry

sam_dict = {
    "mouth": "Mund",
    "finger": "Finger",
    "leg": "Bein",
    "hand": "Hand",
    "face": "Gesicht",
    "nose": "Nase"
}

Entry = input('Enter a word in English or EXIT: ')

while Entry == 'EXIT':
    break
if Entry in sam_dict:
    print('Translation:', sam_dict[Entry])
elif Entry not in sam_dict:
    print('No match!')

I want this to do nothing and exit if they type “EXIT”. I want the loop to cycle back to the input after they enter any word besides “EXIT”.

When I test it, it doesn’t break, it just sees “EXIT” as not in the dictionary and provides “No match”.

Also, even when I re-add the input line at the end of the if and elif statement it doesn’t repeat the loop just prints the input with nothing to follow.

I guess I’m not really understanding how the loop works and where you can put things in the loop? So confused :frowning:

maybe,

sam_dict = {"mouth": "Mund", "finger": "Finger", "leg": "Bein", "hand": "Hand", 
            "face": "Gesicht", "nose": "Nase"}

while ((entry := input('Enter a word in English or EXIT: ')) != 'Exit'):
  print('Translation:', sam_dict[entry]) if entry in sam_dict else print('No match!')

You’re close, but not quite there.

  1. Initialize the Entry variable so that the while loop can test it.
  2. Move the input so that it’s the first operation of the while loop, then test for EXIT.
  3. Set up a if / elif / else branch routine (inside of the while loop), so that you have three tests. Hint: the first test will be for EXIT, then for a word in your dictionary, then for everything else, which will loop back unless we have an EXIT condition.

I’ve left out some detail here, so that you will fall over it and learn more by figuring it out, but if you’d rather have a complete solution, simply say as much.

The so-called Walrus operator that @vainaixr has suggested is one way, but, as a beginner, I would suggest that you acknowledge the existence of that operator, while at the same time avoid using it for now: it’s a relatively new operator, not widely used and not available with Python versions below 3.8

It’s a shame that you didn’t continue with your flights / connections project…

… as you would have had to have learned these kind of tests and loops, while developing that, still never mind.

edit done for a correction.

Thank you I appreciate that and you are correct that I learn better by trial and error.

What is hanging me up is putting an input inside of the while loop:

while
input(input('Enter a word in English or EXIT: ')
if Entry == 'EXIT':
    break
elif Entry in sam_dict:
    print('Translation:', sam_dict[Entry])
else Entry not in sam_dict:
    print('No match!')

This is how my brain is trying to envision this having never seen an example of an input inside of a while loop before.

Those are from an instruction video I’m taking and test examples I’m trying to work through and understand… not really a specific project. My brain is too old for wrote memorization so I have to learn the how and all the why’s behind it all LOL

I can 100% relate to where you’re coming from: I’m very much a ‘why’ person and it got me into bother, in my younger years.

It’s the indentation that dictates the way in which parts of the code are related; it’s not simply for readability or a matter of style.

while condition:
    # this is a part of the while loop
    # and so is this
    if condition:
        # this is not only a part of the while loop, but also apart of the if branch
    # this is outside of the if branch, but still inside the while loop

Note: the above conditions are “rational”, that is to say, based on logic or reason.

Does the above help?

edit to add…

The above is not entirely correct, because this kind of code is also valid:

if 1 == 1: print ("True")

But, for now, go with the indentation style.

{edited to make the explainer clear}

Input In [1]
while:
^
SyntaxError: invalid syntax

I can just have a “while:” it need more. I look it up online and find fancy examples with a bunch of other stuff I don’t understand so it’s not helpful. Is there terminology for the expanded basic version of syntax that I can include in my searches?

Let me get you started, if I may…

Entry = ' ' # this needs to exist, so that it can be tested

while Entry:
    Entry = input('Enter a word in English or EXIT: ')

To explain, what we’ve done there is to say that Entry is equal to a space character, which means it’s not Null and so will return True when tested by the while loop, which says, while Entry is not Null, do the following.

The rest of your code should now fall in with the indentation I described, but there’s a little more for you to discover yet.

edit to add:

This is a very good ‘go to’ site for most of what you’ll need to learn

Thank you for the explanation. I am using W3 combined with my online video course. I really like W3 but I sometimes fail to get the correlation to “this must be here because if not, then there is nothing to check for”.

So I modified my code to the following:

    "mouth": "Mund",
    "finger": "Finger",
    "leg": "Bein",
    "hand": "Hand",
    "face": "Gesicht",
    "nose": "Nase"
}
Entry = ' '

while Entry: 
	Entry = input('Enter a word in English or EXIT: ')
	if Entry == 'EXIT':
    	break
	elif Entry in sam_dict:
    	print('Translation:', sam_dict[Entry])
	elif Entry not in sam_dict:
    	print('No match!')

but I got an error at the break. for “inconsistent use of tabs and spaces in indentation”. My spacing looks identical to the examples in W3 schools etc.

So I went line by line and backed it all up against the wall and started again from there using only tabs (I’m going from Carnets on my phone with no tab button to computer to play with it throughout the day).

sam_dict = {
	"mouth": "Mund",
	"finger": "Finger",
	"leg": "Bein",
	"hand": "Hand",
	"face": "Gesicht",
	"nose": "Nase"
}
Entry = ' '
while Entry: 
	Entry = input('Enter a word in English or EXIT: ')
	if Entry == 'EXIT':
		break
	elif Entry in sam_dict:
		print('Translation:', sam_dict[Entry])
	elif Entry not in sam_dict:
		print('No match!')

this worked in an online python tester so I guess it’s fixed now, Thank you!

You are very welcome. Trust that I provided more help than confusion.

The reason for the line Entry = ' ' becomes clear, if you remove it: as soon as the script hits: while Entry:, it crashes with…

builtins.NameError: name 'Entry' is not defined

Indents of four spaces are very common practice. Some text editors will replace a ‘tab’ character with four spaces, which is really what you need to do; don’t use the ‘tab’ character in your code.

Have a play around with this code and see if you can have it ‘exit’ when a user simply presses the enter key, with no input, but loops back for more input unless the input is empty, i.e Null: I’m sure you can figure it out, if not, then once again post back and you’ll get help with it.

You might find these with: structures to be clearer:

while True:  #equivalent to the 'while Entry' form  because nonempty primitive objects evaluate to boolean True
    Entry = input("Type a word: ")
    if Entry = 'EXIT':
        break

OR…

while Entry != 'EXIT':
    # code here but...
    # now you'll have to be
    # very careful about where
    # the input() statement is, which is
    # a great learning exercise.

With while True: you can create indefinite loops and then break out of them explicitly–at the exact line where you need to stop executing code.

2 Likes