首页 > 解决方案 > 比较数字给出错误答案

问题描述

first_num, second_num = input("Enter the first number: "), input("Enter the second number: ")

if first_num > second_num :
    print(first_num, ' is the greatest number.')

else :
    print(second_num, ' is the greatest number.')

标签: pythonif-statement

解决方案


您正在比较两个字符串,而不是整数或浮点数。您必须将输入转换为 int 或您想要的任何其他格式,然后进行比较。所以完整的代码应该是:

first_num, second_num = int(input("Enter the first number: ")), 
int(input("Enter the second number: "))

if first_num > second_num :
    print(first_num, ' is the greatest number.')

else :
    print(second_num, ' is the greatest number.')

推荐阅读