[英]Write a program that compares the three numbers and prints the largest odd number
[英]How to write a function that prints the highest number out of three numbers
我需要编写一个 function ,它将返回三个数字中的最高数字。
我用这段代码试了一下:
# 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)
幸运的是,代码正在运行。 但我想知道是否有任何更简单或更简单的方法来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.