Shuhana
Shuhana

Reputation: 11

"TypeError: 'str' object cannot be interpreted as an integer"

I am creating n number of students name but I can't . My source code is:

n= input ("enter number of students")
for I in range(0,n):
    Stud_name=input ("enter the name of students")
    Print(stud_name)

Upvotes: 1

Views: 629

Answers (1)

tdelaney
tdelaney

Reputation: 77347

Users enter text and the program needs to parse that into an integer. Accounting for users who can't follow simple instructions...

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)

Upvotes: 2

Related Questions