python - 如何编写一个打印三个数字中最大数字的函数
问题描述
我需要编写一个函数来返回三个数字中最大的数字。
我用这段代码试了一下:
# The input will run until i reaches 2 (starts from 1)
# i will only get incremented when the input is valid (= is an integer)
i = 1
while i < 2:
try:
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
z = int(input("Enter third number: "))
i += 1
# when the input isn't valid (= is a string), the user must enter a valid value (integer)
except ValueError:
print("You entered a string. Try again.")
# Define the function
def max_of_three(x, y, z):
if x > y and x > z or x == y > z or x == z > y:
print (f"The highest number out of {x}, {y} and {z} is: ", x)
if y > x and y > z or y == z > x:
print (f"The highest number out of {x}, {y} and {z} is: ", y)
if z > x and z > y:
print (f"The highest number out of {x}, {y} and {z} is: ", z)
if x == y == z:
print ("All numbers are equal")
# Output function
max_of_three(x, y, z)
幸运的是,代码正在运行!但我想知道是否有任何更简单或更简单的方法来解决这个问题。
解决方案
推荐阅读
- spartacus-storefront - Spartacus Storefront 上的 CDS(上下文驱动服务)上的自定义事件
- ruby - 如何重定向 $stdout?
- ios - CocoaPods 找不到 pod“Firebase/Everything”的兼容版本
- arrays - 如何将我的数组对象放入材质 ui 表中
- postgresql - 一对多不同的实体类型
- python - 如何从 search_all_iam_policies() 结果中获取 json 表示
- java - Apache CXF wsdl 响应问题
- javascript - 使用映射器的两种方式绑定计算属性
- python - AttributeError:模块 'keras.layers' 没有属性 'regularizers'
- python - 如何在 AWS Lambda 函数中获取 eventName?