#!/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()
实际上我只是想建立一个程序来理解逻辑运算符。问题是它一直抛出这个错误。你知道吗
"File "/home/pi/Murach/randomtests/while test.py", line 10, in ageCheck
if ageCheck.age() >= 65 or age <18:
AttributeError: 'function' object has no attribute 'age'"
多亏了@PatrickArtner,我才能够用构建程序的初始IF语句来实现它。谢谢您!!!你知道吗
agecheck
是一个函数-您访问它的age()
属性,它没有属性-因此出现错误。你知道吗改用
age
。你知道吗注意范围-在
ageCheck()
内的status
不是您的全局status
,而是一个局部变量。使用status
作为string
或bool
取决于函数。你知道吗决定使用哪一个或两个不同的变量作为status(true/false)和status(retired或not)。你知道吗
您可以这样重写一些代码:
为了避免在解析非整数输入时出错,在不需要全局变量的情况下传递变量,并便于逻辑检查。你知道吗
17的输出:
65的输出:
HTH公司
相关问题 更多 >
编程相关推荐