Search code examples
pythonvariables

Execute a Code Depending on Variable Input


I am creating an Enrollment Management Program as an output for our school.

The program I am creating contains a section that asks the user about their family.

One category is how many siblings they have. I want the program to ask the user for the number of siblings they have, and then execute the siblingCheck function that many times.

I tried this solution but it does not work, it continues to ask the user for the same values over and over again:

def siblingCheck():
    input("Name: ")
    input("Age: ")
    input("Grade Level: ")


siblingCount = int(input("Number of Siblings: "))
x = 0
while (x <= siblingCount):
    siblingCheck()
else:
    print("Next Section.")

Solution

  • The trouble is that for any non-negative value of siblingCount, while loop will run forever. You may want use a for loop instead:

    def siblingCheck():
        input("Name: ")
        input("Age: ")
        input("Grade Level: ")
    
    
    siblingCount = int(input("Number of Siblings: "))
    if siblingCount > 0:
        for i in range(siblingCount):
            siblingCheck()
    else:
        print("Next Section.")