错误是 'int' object is not iterable,我正在 python 中做一个小项目,允许用户输入十个学生的名字和十个年龄
the error is 'int' object is not iterable, I am working on a small project in python that allows the user to input ten names and ten ages of students
print("\n")
print("Please input ten student names and their ages as well")
print("\n")
print("Student Names: \t\t\t\t Student Ages: ")
print("------------------- \t\t\t\t -------------------")
print("\n")
studentNames =[]
for element in range(10):
element = input("Please input 10 student names: ")
input_ages = int(input("\t\t\t\t\tNow please input their ages as well: "))
team_leader = max(input_ages)
print(team_leader)
for element in range(10):
...
input_ages = int(input("\t\t\t\t\tNow please input their ages as well: "))
team_leader = max(input_ages)
input_ages
是一个整数,每次循环迭代都会重新分配。
max(input_ages)
期望 input_ages
是一个列表,通过遍历它来找到最大值。因此你得到 'int' object is not iterable
.
要正确执行此操作,请在循环之前初始化一个空列表 input_ages
,然后在循环中将年龄附加到该列表:
input_ages = []
for element in range(10):
...
input_ages.append(int(input("\t\t\t\t\tNow please input their ages as well: ")))
team_leader = max(input_ages)
根据您所写的内容:
Displays info
Makes empty list studentsNames
Repeats the following 10 times:
Asks the user for ten names and put the string in a variable
Asks the user for the ten ages and tries to turn a string to an integer* and stores that in a variable
Each loop, the variables are overwritten
Gets the highest value from the age variable which would be the last and only one, saves it to a variable
Display that variable
*你必须给每个学生输入一个循环,否则你会出错。
但是我想你会想做这样的事情。我继续使用两个列表,但我认为会有更好的方法来存储数据。
print("your info")
names = []
ages = []
for __ in range(10):
name.append(input("Enter student name: "))
age.append(int(input("Age: ")))
oldest_age = max(ages)
index = ages.index[oldest_age]
team_leader = names[index]
print(team_leader)
print("\n")
print("Please input ten student names and their ages as well")
print("\n")
print("Student Names: \t\t\t\t Student Ages: ")
print("------------------- \t\t\t\t -------------------")
print("\n")
studentNames =[]
for element in range(10):
element = input("Please input 10 student names: ")
input_ages = int(input("\t\t\t\t\tNow please input their ages as well: "))
team_leader = max(input_ages)
print(team_leader)
for element in range(10): ... input_ages = int(input("\t\t\t\t\tNow please input their ages as well: ")) team_leader = max(input_ages)
input_ages
是一个整数,每次循环迭代都会重新分配。
max(input_ages)
期望 input_ages
是一个列表,通过遍历它来找到最大值。因此你得到 'int' object is not iterable
.
要正确执行此操作,请在循环之前初始化一个空列表 input_ages
,然后在循环中将年龄附加到该列表:
input_ages = []
for element in range(10):
...
input_ages.append(int(input("\t\t\t\t\tNow please input their ages as well: ")))
team_leader = max(input_ages)
根据您所写的内容:
Displays info
Makes empty list studentsNames
Repeats the following 10 times:
Asks the user for ten names and put the string in a variable
Asks the user for the ten ages and tries to turn a string to an integer* and stores that in a variable
Each loop, the variables are overwritten
Gets the highest value from the age variable which would be the last and only one, saves it to a variable
Display that variable
*你必须给每个学生输入一个循环,否则你会出错。
但是我想你会想做这样的事情。我继续使用两个列表,但我认为会有更好的方法来存储数据。
print("your info")
names = []
ages = []
for __ in range(10):
name.append(input("Enter student name: "))
age.append(int(input("Age: ")))
oldest_age = max(ages)
index = ages.index[oldest_age]
team_leader = names[index]
print(team_leader)