Home > Mobile >  how to write a code that loops more than once using the for loop function in python
how to write a code that loops more than once using the for loop function in python

Time:06-01

here is my code:

number = input("How many students are registering?")

for i in number:
    id_number = input("Enter your ID Number:")
    print(id_number)

with open("reg_form.txt","w") as f:
    f.write(id_number "\n")

the code has to loop for the same number entered in variable "number" in my code above.

CodePudding user response:

this question shows no research effort, but here is what you are looking for:

with open("reg_form.txt","w") as f:
    for i in range(int(input('how many students registering?'))):
        id_number = input("Enter your ID Number:")
        print(id_number)
        f.write(id_number "\n")

CodePudding user response:

this should do the trick (making sure the input is an integer & you iterate over an iterable (range))

number = int(input("How many students are registering?"))

for i in range(number):
    id_number = input("Enter your ID Number:")
    print(id_number)

CodePudding user response:

All you need to do is use a For Loop with number.

Here's the code:

number = input("How many students are registering?")

for i in range(int(number)):
  id_number = input("Enter your ID Number:")
  print(id_number)

  with open("reg_form.txt","w") as f:
    f.write(id_number "\n")

I believe you meant to put the With Statement within the For Loop, because it won't work otherwise (id_number isn't defined).

CodePudding user response:

I don't exactly know what is your problem, but I think i can help you

number = input("How many students are registring")
for i in range(number)
   id_number = input("Enter your ID Number: ")
   print(id_number)
 with open("reg_form.txt","w") as f: f.write(id_number "\n"
   
  • Related