我目前正在创建一个函数,该函数将允许提供用户将输入的最大整数。
代码:
def max_num(x,y,z):
if x>=y and x>=z:
return x
elif y>=x and y>=z:
return y
else:
return z
num1= input("1st value: ")
num2= input("2nd value: ")
num3= input("3rd value: ")
print("The highest integer is:",max_num(num1,num2,num3))
当我尝试输入值300,1000,200时,它总是返回300,显然应该是1000。有人有解决方案吗?
答案 0 :(得分:2)
num1= int(input("1st value: "))
num2= int(input("2nd value: "))
num3= int(input("3rd value: "))
print("The highest integer is:",max_num(num1,num2,num3))
答案 1 :(得分:0)
将字符串转换为整数:
def max_num(x,y,z):
if int(x)>=int(y) and int(x)>=int(z):
return x
elif int(y)>=int(x) and int(y)>=int(z):
return y
else:
return z
num1= input("1st value: ")
num2= input("2nd value: ")
num3= input("3rd value: ")
print("The highest integer is:",max_num(num1,num2,num3))
答案 2 :(得分:0)
只需使用预定义的max()函数即可:
<FlyoutItem Title="HOME" FlyoutDisplayOptions="AsMultipleItems">
<ShellContent Title="Categories & Cards" Icon="fa-th-large">
</ShellContent>
<ShellContent Title="To-Dos" Icon="fa-tasks">
</ShellContent>
</FlyoutItem>
答案 3 :(得分:0)
尝试仅使用max函数
def max_num(x,y,z):
return (max(x,y,z))
答案 4 :(得分:0)
<img src="https://i.stack.imgur.com/HO8EQ.png">
输出
def max_num(*args):
highestNum = 0
for num in args:
if num > highestNum:
highestNum = num
print("higher num has been found " + str(highestNum))
else:
print(str(num) + " is not higher than " + str(highestNum))
max_num(2,3,10,1)