python - Python3.7 input() 连接整数
问题描述
我有以下python代码。
a = input("Enter first number") # 2
b = input("Enter second number") # 3
c = a+b # 23 instead of 5
print(c) # prints out 23 why?
我正在使用以下命令来运行 python:
python3.7 filename.py
不是添加两个数字而是连接两个数字并给我 23 而不是 5,即使我使用的是 python3.7。
我读到的每个答案都说它评估并返回正确的类型:
https://www.quora.com/What-is-the-difference-between-raw_input-and-input-in-Python#
解决方案
a+b
连接字符串作为input
返回字符串。您需要显式类型转换才能使用int()
函数将输入转换为整数。
a = int(input("Enter first number"))
b = int(input("Enter second number"))
推荐阅读
- javascript - 如何注册唯一值?在电子表格谷歌应用脚本网络中
- excel - Vba 抓取 Twitter 详细信息
- artifactory - 有没有办法只备份工件存储库的元数据而不是内容
- javascript - Javascript - 返回初始状态
- php - 无法使用 VPN 和 WSL2 连接 SQL Server 数据库
- sql-server - 如何始终以相同的顺序返回行
- bash - Bash 脚本将日志从一个 pod 分离到另一个
- excel - Adding a note to a cell based on another cell
- node.js - 在 Node.js 模板中加载内联脚本,然后将其发送到客户端
- haskell - 使用 QuickCheck 将 ST 与多态测试“模板”一起使用