[Tutor] decision loops

Steven D'Aprano steve at pearwood.info
Sun Feb 1 11:14:36 CET 2015


Hello Daniel, and welcome! 

On Sat, Jan 31, 2015 at 08:54:48PM -0500, Daniel M wrote:

> Hello. I'm a complete beginner and I’m trying to write a very basic script
> to convert temperatures, just for some practice. I have that part down, but
> I can’t figure out how to make the script switch between the two. What I
> would like it to do is let the user go back to the “What do you wish to
> convert?” part when a character is entered instead of a number for
> “temperature?”. I tried using
> 
> elif m == *char*
> 
>     print (*"What do you wish to convert to?"*)
> 
>     temp = raw_input(*">> "*)

Why are there asterisks around parts of your code? That's a syntax 
error in Python. I'm guessing that you didn't type them yourself, and 
that your email program is doing it. You should set your email 
program to only send plain text, not "Rich Text" or formatted text or 
HTML or whatever silly thing it is trying to do.


> but it seems useless regardless of where I put it. It gives me the error “
> return eval(raw_input(prompt))

In the code you show below, there is no mention of eval. So that is your 
first mistake: whether you intended to or not (I'm pretty sure it wasn't 
deliberate!) you are telling us falsehoods. The code you say you are 
running is not the code you are actually running. How can we tell what 
you are doing wrong when we cannot see what you are doing?

Mistake number two is using eval. As a beginner, there are three rules 
you should remember about eval:

(1) If you think you might need to use eval, you don't.
(2) If you are positive that you really do need to use eval, 
    you probably don't.
(3) For experts only -- if you are sure that you need to use 
    eval, you might.


The problems with eval are:

- its slow
- its tricky to use right except for the simplest cases
- it can be dangerous and introduce serious security holes 
  in your code if you aren't careful


>   File "<string>", line 1, in <module>
> 
> NameError: name 't' is not defined” when I enter a character.

Mistake number three: I'm guessing that you didn't enter any old 
character. I'm guessing you entered 't' rather than 's' or '3' or '.' or 
some other character. Your mistake is to make us guess: when asking for 
help, you should tell us what you did specifically, not vaguely.

In this case, I think I can reproduce your problem:

py> eval(raw_input("Enter a character: "))
Enter a character: t
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 't' is not defined


I don't get that error from any random character, only from t. If I type 
a different character, I get a different error. Why? Because of eval. 
You're telling Python to evaluate what you typed as if it were code: 

py> eval(raw_input("Enter a character: "))
Enter a character: len([1, 2, 3]) + 1000
1003

You don't need eval here. It does nothing useful. What you want is 
simply raw_input(prompt).

Hope that helps.

-- 
Steve


More information about the Tutor mailing list