Python 你怎么称呼参数?
我想先把论点打印出来Python 你怎么称呼参数?,python,function,arguments,Python,Function,Arguments,我想先把论点打印出来 # I want to call it here. print("Which of these values is the highest?") # Without needing to print this one below. How do I call the argument at the end? print(4, 8, 2) def max_num(num1, num2, num3): if num1 >= num2 and
# I want to call it here.
print("Which of these values is the highest?")
# Without needing to print this one below. How do I call the argument at the end?
print(4, 8, 2)
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
print(max_num(4, 8, 2), "is the highest value.")
我知道我可以创建一个变量并改变整个过程,但这里有可能吗?如果需要多次使用赋值给变量
# --- functions ---
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
# --- main ---
n1 = 4
n2 = 8
n3 = 2
print("Which of these values is the highest?")
print(n1, n2, n3)
print(max_num(n1, n2, n3), "is the highest value.")
顺便说一句:如果您将其保留为列表,则可以使用标准的
max(list)
而不是您的函数
numbers = [4, 8, 2]
print("Which of these values is the highest?")
print(*numbers) # use * to unpack values from list and use as separated values in `print()`
print(max(numbers), "is the highest value.")
是否总是只有3个数字或函数应该接受许多数字作为输入?为什么我没有想到lmao,非常感谢你为什么不先分配变量-然后你可以在任何地方使用它。或者最好将其作为列表保存在变量中-然后您可以使用标准
max(list)