python中的此代码段未执行。有人能指出错误吗?你知道吗
print("Lets have a math quiz")
def True_False(x):
if True :
print("You are absolutely correct")
else :
print("Sorry you are wrong better luck next time ")
i =input("what is 4 * 4 = ")
if i == "16":
True_False(True)
else:
True_False(False)
i =input("what is 7+ 60 = ")
if i == "76":
True_False(True)
else:
True_False(False)
i =input("what is 4 * 0 = ")
if i == "0":
True_False(True)
else:
True_False(False)
您的主要问题是
True_False
函数。if True: ...
将始终采用if
分支,而从不采用else
分支,根本不检查x
参数。你知道吗您需要更改
if
语句,以便它取决于参数,并且您可能应该为函数和参数提供更具描述性的名称:其次,在主程序中调用此函数时,
if i == ...
检查是多余的。如果i
是16
,那么i == 16
将计算为True
,因此您可以简单地将其传递给函数:或者,您可以重构函数,以便它接受用户的猜测和正确答案,并自行执行检查:
相关问题 更多 >
编程相关推荐