[Tutor] help with and

Alan Gauld alan.gauld at yahoo.co.uk
Wed Mar 1 04:29:04 EST 2017


On 01/03/17 06:21, darrickbledsoe at gmail.com wrote:

> wage = eval(input("Enter in the employees hourly wage: ")) #get wage
> hours_worked = eval(input("Enter in the number of hours worked: "))

Don't use eval() like this it is a security risk
and is a very bad habit to get into. Instead use
an explicit type conversion such as int() or float().

> pay = wage * hours_worked # calculate pay
> ot = ((hours_worked - 40 ) *  (1.5) * wage) + (wage * 40) 
> double = ((hours_worked - 40 ) *  (2) * wage) + (wage * 40) 

You don't need all those parentheses around the terms,
especially the numbers.

> if (hours_worked <= 40):
>     print (pay)
> 
> if (hours_worked > 40 and < 60):
>     print (ot)

Python sees this as:

   if (hours_worked > 40) and (< 60):

And doesn't know what is intended to be less than 60.

You need to be explicit:

   if hours_worked > 40 and hours_worked < 60:

You can also write what you intend in a slightly
different form:

   if (40 < hours_worked < 60):

Note:
The second form an unusual style that I've only
ever seen in Python, most languages insist you
use the first version.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list