[英]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.