describe('DataService', () => {
let service: DataService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [DataService]
});
// inject the service
service = TestBed.get(DataService);
httpMock = TestBed.get(HttpTestingController);
});
it('can test for server error', () => {
service.createEmployee(MOCK_EMPLOYEE[0]).subscribe((response) => {
fail('should not respond with new employee')
},
(error: HttpErrorResponse) => {
expect(error.error.status).toEqual(500);
});
let call: TestRequest =
httpMock.expectOne(`${service.baseUrl}/employees`);
expect(call.request.method).toEqual('POST');
call.error(
new ErrorEvent('Server error'), { status: 500 }
);
httpMock.verify();
});
}
我实际上只是试图构建一个程序来理解逻辑运算符。问题是它不断抛出此错误。
#!/usr/bin/env python3
status = False
while status == True:
status == "retired"
def ageCheck():
age = int(input("Enter Your Age: "))
if ageCheck.age() >= 65 or age <18:
status = True
def discountCheck():
if (ageCheck.age() >= 65 and status == "retired") or ageCheck.age() < 18:
print("You get 5% off")
def welcome():
print()
print("Welcome to Age Test")
welcome()
ageCheck()
discountCheck()
答案 0 :(得分:1)
agecheck
是一个函数-您访问其age()
属性,该属性没有任何属性-因此出错。
改为使用age
。
当心作用域-status
中的ageCheck()
不是全局status
,而是局部变量。您可以将status
用作string
或bool
-取决于您的功能。
确定状态(是/否)和状态(是否退休)中的一个或使用2个不同的变量。
您可以像这样重写一些代码:
#!/usr/bin/env python3
def getAge():
while True:
age = input("Enter Your Age: ")
if age.isdigit() and int(age) > 0:
return int(age)
# repeat until number given
def isElegibleForDiscount(someAge, isRetired):
return someAge < 18 or someAge >= 65 and isRetired # operator precedence, no () needed
def discountCheck(myAge,myStatus):
if isElegibleForDiscount(myAge, myStatus == "retired"):
print("You get 5% off")
else:
print("No discount, sorry.")
def welcome():
print()
print("Welcome to Age Test")
welcome()
age = getAge()
discountCheck(age,"retired")
discountCheck(age,"still kickin")
避免在解析非整数输入时出错,无需全局变量就传递变量,并简化逻辑检查。
17的输出:
Welcome to Age Test
Enter Your Age: 17
You get 5% off
You get 5% off
输出为65:
Welcome to Age Test
Enter Your Age: 65
You get 5% off
No discount, sorry.
HTH
答案 1 :(得分:0)
由于@PatrickArtner,我能够使用构建程序时所用的初始IF语句来完成此操作。谢谢!!!
#!/usr/bin/env python3
def statusCheck(somage):
if somage >= 65:
return True
else:
return False
def ageCheck():
while True:
age = input("Enter Your Age: ")
if age.isdigit() and int(age) > 0:
return int(age)
def discountCheck(someage, mystatus):
if (someage >= 65 and mystatus == "isAble") or someage < 18:
print("You get 5% off")
else:
print("Age inacceptable")
def welcome():
print()
print("Welcome to Age Test")
welcome()
age = ageCheck()
statusCheck(age)
def ifstatus():
if statusCheck(age) == True:
status = "isAble"
return str(status)
status = ifstatus()
discountCheck(age, status)