我对python很陌生,我试图构建一个程序来评估潜在的合作伙伴是否太年轻,不适合使用/2+7规则的人。
尽管使用了18以上的测试变量,但无论我做什么,程序都会执行第7行。我用了88/77,77/66,19/19,它总是执行第7行。
num1 = float(input("What is the higher age number? "))
num2 = float(input("What is the lower age number? "))
output = num1 / 2 + 7
if num1 and num2 <= 17:
print("You're both underage")
elif num2 <= 17:
print("You're going to jail bud")
elif output <= num2:
print("That's OK")
else:
print("They are slightly too young for you")编辑:
我做了很多人建议的修复,但现在这个程序仍然没有按预期工作,我发现了另一个缺陷。
num1 = float(input("What is the higher age number? "))
num2 = float(input("What is the lower age number? "))
output = num1 / 2 + 7
if num1 <= 17 and num2 <= 17:
print("You're both underage")
elif num2 <= 17:
print("You're going to jail bud")
elif output <= num2:
print("That's OK")
else:
print("They are slightly too young for you")当num1 = 19和num2 = 16时,当我希望它输出到第7行时,程序输出第5行。当num1和num2都设置为大于17的值时,它仍然输出第7行。
发布于 2019-01-09 10:24:46
要用Python编写num1 and num2 <= 17,您需要显式地:
if num1 <= 17 and num2 <= 17:
# do something否则,如果是num1 != 0,条件将始终是True。
或者,只需将两个值的max用于等效逻辑:
if max(num1, num2) <= 17:
# do somethinghttps://stackoverflow.com/questions/54107801
复制相似问题