[Tutor] help with and

Peter Otten __peter__ at web.de
Wed Mar 1 04:41:18 EST 2017


darrickbledsoe at gmail.com wrote:

> For some reason I am getting a syntax error when I try and write my second
> If statement. I cannot find anything wrong with the statement because it
> is set up the same as all the others I see online. Perhaps someone can
> inform me why I am getting this. Thanks for your help
> 
> Darrick Bledsoe II
> 
> # -*- coding: utf-8 -*-
> """
> Created on Sat Sep 12 19:23:21 2015
> 
> @author: Darrick Bledsoe
> """
> 
> 
> wage = eval(input("Enter in the employees hourly wage: ")) #get wage

It is safer to write

wage = float(input(...))

if you want a float value. With eval() a user can do nasty things like 
manipulating your data:

$ echo "hello" > precious.txt
$ cat precious.txt
hello
$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> wage = eval(input("enter wage "))
enter wage __import__("os").remove("precious.txt")
>>> 
$ cat precious.txt
cat: precious.txt: No such file or directory

Oops, the file is gone. Even if you write a script for your personal use you 
should make it a habit to use eval() only if there are significant 
advantages.

> if (hours_worked > 40 and < 60):

"< 60" is not a complete expression in Python; you cannot chain it with 
"and" etc. Possible working alternatives are

if hours_worked > 40 and hours_worked < 60:
   ...

and

if 40 < hours_worked < 60:
    ...



More information about the Tutor mailing list