Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have defined a function eval_loop in which I prompt the user with a command in which I ask him to evaluate an expression. The expression I want to evaluate is "2+2". If the user presses done or Done, the while loop will exit. Why am I not able to receive any output when I type "2+2"? Why is the eval command not showing any output?

What I have tried:

def eval_loop ():
    while True:
        line=input("enter the expression you want to evaluate:")
        if line == "done" or "Done":
        print (done)


1 solution

Because this test always passes:
if line == "done" or "Done":
because in Python any non-empty string is True and the second part of your condition is just a string literal.
Change it to this:
if line == "done" or line == "Done":
and it'll get to your next error (which will be "name 'done' is not defined.")

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at over an hour for me to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
Share this answer
Baziga 15-Oct-23 1:50am    
I am not able to detect my syntax errors myself.
OriginalGriff 15-Oct-23 1:59am    
Read the link I gave you: it shows you how to handle syntax errors and get information from them that helps you to fix them without help.

It's not complicated, but it saves you hours and hours of waiting for others to notice you need help and fix them for you. It really is worth trying to fix them yourself first even right at the beginning!
Baziga 15-Oct-23 1:54am    
I will need help for another two or three months. Then, I will stop asking questions on syntax errors. I have hardly spent three hours learning python. I never knew that in python any non-empty string is True.
OriginalGriff 15-Oct-23 2:00am    
"Then, I will stop asking questions on syntax errors."
Don't make promises you can't keep! :D
I still to this day sometimes read what I meant to write instead of what I did ...
Baziga 15-Oct-23 4:23am    
Thank you so much OriginalGriff. I will read that link, in sha Allah!

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