python - 比较数字给出错误答案
问题描述
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.')
解决方案
您正在比较两个字符串,而不是整数或浮点数。您必须将输入转换为 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.')
推荐阅读
- php - 我该如何解决这个错误 errno: 150 “外键约束的格式不正确”
- python - 10折交叉验证python
- html - 仅保留主页的 HTML 标头
- firefox - 量角器:Firefox 浏览器打开,但网站无法访问
- r - 如何一次将数据聚合到不同的值
- java - System.out.println() 总是显示类而不是值
- azure-devops - 从 Azure DevOps Pipeline 祖父 Pipeline 下载工件
- node.js - 如果在猫鼬中使用聚合和 $lookup,如何过滤?
- reactjs - 为什么我的应用程序在桌面上的性能非常好,但在使用 ReactJs 的手机上却搞砸了?
- android - 如何解决“发现现有项目依赖项不一致”的问题?