有人可以扫描我写的这个Python代码并让我知道我做错了什么吗?

时间:2015-09-14 23:36:20

标签: python loops if-statement logic indentation

我必须为我的计算机逻辑课程编写一个程序,但我无法弄清楚为什么它不能运行。我不断得到的错误是IndentationError: expected an indented block (<string>, line 3)有人可以指出我正确的方向吗?如果您发现任何错误,请告诉我。此代码必须能够正常运行。感谢。

while True:

cname = 'My Company'
drate = .17
maxhrs = 40
pdate = "9/1/2015"
orate = 1.5
lc = 'Y'
while(lc == 'Y'):
ename = raw_input("Enter employee's name.")
dcode = raw_input("Enter department code.(1 - Shipping 2 - Mngmt.)")
hworked = float(raw_input("Enter total hours worked."))
prate = float(raw_input("Enter your pay rate."))
inscost = float(raw_input("Enter the cost of insurance."))
city = float(raw_input("Enter your city code.(1 - Sumiton 2 - Hamilton)"))
st = raw_input("Enter your state")
sex = raw_input("Enter your sex.(M - Male F - Female)")
yrsemp = raw_input("Enter total years employed.")

print("Welcome to our company: ", cname)
print("Pay period date: ", pdate)
    if(sex == 'M'):
        sexword = 'Male'
        else:
        sexword = 'Female'
print("Employee name: ", ename, "Sex: ", sexword)
    if(city == '1'):
        cityn = 'Sumiton'
        else:
        cityn = 'Hamilton'
print("City: ", cityn, "State: ", state)
    if(dcode == '1'):
        dname = 'Shipping'
        else:
        dname = 'Management'
print("Department name: ", dname)
rpay = maxhrs * prate
print("Regular pay: ", rpay)
opay = [(maxhrs - hworked) * orate] * prate
print("Overtime pay: ", opay)
gross = rpay + opay
print("Gross before deduction: ", gross)
damt = drate * gross
print("Deduction amount: ", damt "Insurance cost: ", inscost)
npay = gross - (damt + icost)
print("Net pay: ", npay)
new = raw_input("Would you like to start over with a new person? yes/no")
    if(new = 'yes')

restart = int(input("Press 1 to try again, 0 to exit. "))
    if(restart == '1'):
        continue
        elif(restart == '0'):
        break       
        else:
        print("Invalid input. Please enter 1 to restart or 0 to exit.")

`

2 个答案:

答案 0 :(得分:1)

第一次使用后,您的代码缺少缩进。你需要缩进将在这个循环中运行的语句,同样在第9行时对第二个语句执行相同的操作。

答案 1 :(得分:0)

在Python中,冒号必须缩进(推荐4个空格)。所以缩进应该是这样的:

while True:
    cname = 'My Company'
    drate = .17
    maxhrs = 40
    pdate = "9/1/2015"
    orate = 1.5
    lc = 'Y'

while(lc == 'Y'):
    ename = raw_input("Enter employee's name.")
    dcode = raw_input("Enter department code.(1 - Shipping 2 - Mngmt.)")
    hworked = float(raw_input("Enter total hours worked."))
    prate = float(raw_input("Enter your pay rate."))
    inscost = float(raw_input("Enter the cost of insurance."))
    city = float(raw_input("Enter your city code.(1 - Sumiton 2 - Hamilton)"))
    st = raw_input("Enter your state")
    sex = raw_input("Enter your sex.(M - Male F - Female)")
    yrsemp = raw_input("Enter total years employed.")

print("Welcome to our company: ", cname)
print("Pay period date: ", pdate)

if(sex == 'M'):
    sexword = 'Male'
else:
    sexword = 'Female'
print("Employee name: ", ename, "Sex: ", sexword)

if(city == '1'):
    cityn = 'Sumiton'
else:
    cityn = 'Hamilton'
print("City: ", cityn, "State: ", state)

if(dcode == '1'):
    dname = 'Shipping'
else:
    dname = 'Management'
print("Department name: ", dname)

rpay = maxhrs * prate
print("Regular pay: ", rpay)
opay = [(maxhrs - hworked) * orate] * prate
print("Overtime pay: ", opay)
gross = rpay + opay
print("Gross before deduction: ", gross)
damt = drate * gross
print("Deduction amount: ", damt "Insurance cost: ", inscost)
npay = gross - (damt + icost)
print("Net pay: ", npay)
new = raw_input("Would you like to start over with a new person? yes/no")
if(new = 'yes')

restart = int(input("Press 1 to try again, 0 to exit. "))
if(restart == '1'):
    continue
elif(restart == '0'):
    break
else:
    print("Invalid input. Please enter 1 to restart or 0 to exit.")

(顺便说一下,你的代码有很多问题......我甚至不能理解这是Python 3或Python 2 ......)