如果使用 OR 或 AND 门,如何表示嵌套?
How to represent nested if using OR or AND gates?
a=int(input("Enter a"))
b=int(input("Enter b"))
c=int(input("Enter c"))
if(a>b and a>c):
print("a is greatest")
elif(b>a and b>c):
print("b is greatest")
else:
print("c is greatest")
这是我的代码,用于查找 3 个数字之间的最大数。
我的课本上写了不同的代码,我想把它转换成我的代码类型。
课本上写的代码如下-:
num1=int(input("Enter num1"))
num2=int(input("Enter num2"))
num3=int(input("Enter num3"))
if(num1>num2):
if(num1>num3):
print(num1,"is greater than",num2,"and",num3)
else:
print(num3, "is greater than", num1, "and", num2)
elif(num2>num3):
print(num2,"is greater than",num1,"and",num3)
else:
print("The three numbers are equal")
我想在if条件和elif条件中将这个if语句转换成布尔表达式,我该怎么做?
这是一个动态的方法。它有点复杂,但效果很好。
如果你仔细阅读它,那么你就会明白它。
items = [int(input("Enter a: ")), int(input("Enter b: ")), int(input("Enter c: "))]
highest = items[0]
letter = "A"
for index, item in enumerate(items):
if item > highest:
highest = item
letter = chr(ord("A")+index)
print("{letter} has the highest value of {highest}".format(letter=letter, highest=highest))
if (num1>num2) and (num1>num3):
print(num1,"is greater than",num2,"and",num3)
elif (num3>num1) and (num3>num2):
print(num3, "is greater than", num1, "and", num2)
elif (num2>num3) and (num2>num1):
print(num2,"is greater than",num1,"and",num3)
else:
print("The three numbers are equal")
您可以随时使用 max(num1,num2,num3)
功能,但我猜您不想要那个。
编辑:
在您书中的示例中,在外部 if 和其嵌套的 if-else 之间有一个隐式的 AND 运算符。所以
if(num1>num2):
if(num1>num3):
print(num1,"is greater than",num2,"and",num3)
else:
print(num3, "is greater than", num1, "and", num2)
实际上等同于
if(num1>num2) and (num1>num3):
print(num1,"is greater than",num2,"and",num3)
if(num1>num2) and (num1<=num3):
print(num3, "is greater than", num1, "and", num2)
同样,
elif(num2>num3):
print(num2,"is greater than",num1,"and",num3)
相当于
if(num1<=num2) and (num2>num3):
print(num2,"is greater than",num1,"and",num3)
a=int(input("Enter a"))
b=int(input("Enter b"))
c=int(input("Enter c"))
if(a>b and a>c):
print("a is greatest")
elif(b>a and b>c):
print("b is greatest")
else:
print("c is greatest")
这是我的代码,用于查找 3 个数字之间的最大数。
我的课本上写了不同的代码,我想把它转换成我的代码类型。
课本上写的代码如下-:
num1=int(input("Enter num1"))
num2=int(input("Enter num2"))
num3=int(input("Enter num3"))
if(num1>num2):
if(num1>num3):
print(num1,"is greater than",num2,"and",num3)
else:
print(num3, "is greater than", num1, "and", num2)
elif(num2>num3):
print(num2,"is greater than",num1,"and",num3)
else:
print("The three numbers are equal")
我想在if条件和elif条件中将这个if语句转换成布尔表达式,我该怎么做?
这是一个动态的方法。它有点复杂,但效果很好。
如果你仔细阅读它,那么你就会明白它。
items = [int(input("Enter a: ")), int(input("Enter b: ")), int(input("Enter c: "))]
highest = items[0]
letter = "A"
for index, item in enumerate(items):
if item > highest:
highest = item
letter = chr(ord("A")+index)
print("{letter} has the highest value of {highest}".format(letter=letter, highest=highest))
if (num1>num2) and (num1>num3):
print(num1,"is greater than",num2,"and",num3)
elif (num3>num1) and (num3>num2):
print(num3, "is greater than", num1, "and", num2)
elif (num2>num3) and (num2>num1):
print(num2,"is greater than",num1,"and",num3)
else:
print("The three numbers are equal")
您可以随时使用 max(num1,num2,num3)
功能,但我猜您不想要那个。
编辑:
在您书中的示例中,在外部 if 和其嵌套的 if-else 之间有一个隐式的 AND 运算符。所以
if(num1>num2):
if(num1>num3):
print(num1,"is greater than",num2,"and",num3)
else:
print(num3, "is greater than", num1, "and", num2)
实际上等同于
if(num1>num2) and (num1>num3):
print(num1,"is greater than",num2,"and",num3)
if(num1>num2) and (num1<=num3):
print(num3, "is greater than", num1, "and", num2)
同样,
elif(num2>num3):
print(num2,"is greater than",num1,"and",num3)
相当于
if(num1<=num2) and (num2>num3):
print(num2,"is greater than",num1,"and",num3)