Example of Python code (C code to Python code)

Robert Roy rjroy at takingcontrol.com
Tue Jun 6 22:40:16 EDT 2000


On Tue, 6 Jun 2000 21:08:45 -0400, "Robin Porter" <mogul at primus.ca>
wrote:

Here is one way, fairly straightforward translation.
Does no error checking on input so if you enter an invalid value at
the prompt will raise exception.
##################

TaxRate = 0.23
MinPay = 300.0
RegHours = 40.0

HoursWorked = float(raw_input('Please enter the hours worked per week
==> '))
PayRate = float(raw_input('Please enter the hourly rate of pay ==> '))

if HoursWorked > RegHours:
    GrossPay = (RegHours * PayRate) + ((HoursWorked-RegHours) *
PayRate * 1.5)
else:
    GrossPay = HoursWorked * PayRate

if GrossPay > MinPay:
    NetPay = GrossPay - ((GrossPay-MinPay)*TaxRate)
else:
    NetPay = GrossPay
    
print 'Gross Pay', GrossPay
print 'Net Pay', NetPay    

##############
#  Just for fun
# Here is another way, playing around a bit to minimise line count

TaxRate, MinPay, RegHours  = 0.23, 300.0, 40.0

HoursWorked = float(raw_input('Please enter the hours worked per week
==> '))
PayRate = float(raw_input('Please enter the hourly rate of pay ==> '))

print 'Gross Pay', (min(HoursWorked,RegHours) * PayRate) +
(max(0,(HoursWorked-RegHours)) * PayRate * 1.5)  
print 'Net Pay', GrossPay - (max(0,(GrossPay-MinPay)) * TaxRate) 
 
##########
Bob


>Could someone please give me an example of some Python code.  I am just new
>to this language and would appreciate if you could re-write this simple
>piece of C code in Python.
>
>Thank you,
>
>Robin
>
>
>#include "stdafx.h"
>#include <stdio.h>
>#include <math.h>
>
>float
>HoursWorked,PayRate,Overtime,IncomeTaxes,GrossPay,NetPay,TaxPayable,Overtime
>Hours;
>
>const float TaxRate=.23;
>const MinPay=300;
>const RegHours=40;
>
>main()
>{
>
>TaxPayable=0;
>Overtime=0;
>OvertimeHours=0;
>
> printf("\nPlease enter the hours worked per week? ");
> scanf("%f",&HoursWorked);
>
> printf("\nPlease enter the rate of pay? ");
> scanf("%f",&PayRate);
>
> if (HoursWorked>RegHours)
> {
>  OvertimeHours =HoursWorked-RegHours;
>  Overtime = (OvertimeHours)*(PayRate*1.5);
> }
>
> GrossPay = ((HoursWorked-OvertimeHours)*PayRate)+Overtime;
>
> if (GrossPay>MinPay)
> {
>  TaxPayable = (GrossPay-MinPay)*TaxRate;
> }
>
> NetPay = (GrossPay-TaxPayable);
>
> printf("\nThe gross pay is %f",GrossPay);
> printf("\nThe net pay is %f",NetPay);
>
>
> return 0;
>}
>
>
>




More information about the Python-list mailing list