繁体   English   中英

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

[英]Can someone scan over this Python code that I wrote and let me know what I did wrong?

我必须为我的计算机逻辑课程编写一个程序,但我不知道为什么它无法运行。 我不断收到的错误是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.")

`

您的代码在第一次之后缺少缩进。 您需要缩进将在此循环中运行的语句,并在第 9 行对第二个语句执行相同操作。

在 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......)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM