python-3.x - “TypeError:'str' 对象不能解释为整数”
问题描述
我正在创建 n 个学生的名字,但我不能。我的源代码是:
n= input ("enter number of students")
for I in range(0,n):
Stud_name=input ("enter the name of students")
Print(stud_name)
解决方案
用户输入文本,程序需要将其解析为整数。考虑到无法遵循简单说明的用户...
while True:
try:
n = int(input("Enter number of students: "))
break
except ValueError:
print("Invalid number")
for i in range(1, n+1):
stud_name = input("Enter the name of student {}: ".format(i))
print(stud_name)
推荐阅读
- swift - 如何在 MacOS 上使用 IPC 将字符串从应用程序发送到 DAL 插件?
- vim - 如何使 shift+space 快捷方式转到 vim 中的行尾?
- python - 如何使用matplotlib检测是否有交集?
- wordpress - 如何尝试转换这个可能包含一些奇怪代码的 API 查询参数?
- flutter - 颤振过高的速率
- python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:'aria2c'
- extjs - Sencha Cmd [ERR] no !/ in spec
- lua - Roblox Studio 价值问题
- cmake - 在cmake中获取可执行文件之间的依赖关系
- java - 您的 SQL 语法有错误 检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法使用