Hi, im trying to find a way to repeat this code with out losing the count of 1,2,3 and 4, when the user enters a number different from 1,2,3 or 4. Because in line 26 I call for main() again but when I do that it loses count of everything.

def main():
    print "WELCOME TO THE GRADE LEVEL CALCULATOR!"
    print
    
    x = 0
    f = 0.0
    s = 0.0
    j = 0.0
    se = 0.0
    nstudents = 0

    while x <= 4:

        print "Please choose the class standing from the following menu:"
        print "     1) Freshman"
        print "     2) Sophomore"
        print "     3) Junior"
        print "     4) Senior"
        print "     5) Quit"
        print

        x = input("Please enter your choice here: ")

        if x > 5 or x < 1:
            print "Invalid choice. Must choose 1 - 5."
            main()
            print
        
        elif x == 1:
            f = f + 1
            nstudents = nstudents + 1
    
        
        elif x == 2:
            s = s + 1
            nstudents = nstudents + 1
        elif x == 3:
            j = j + 1
            nstudents = nstudents + 1
        elif x == 4:
            se = se + 1
            nstudents = nstudents + 1
        elif x == 5:

            print "Total number of students entered: ", nstudents

            total = f + s + j + se
            fper = (f / total ) * 100
            sper = (s / total ) * 100
            jper = (j / total ) * 100
            seper = (se / total ) * 100

            print "Freshman:      ", fper,"%"
            print "Sophomores:    ", sper,"%"
            print "Juniors:       ", jper,"%"
            print "Seniors:       ", seper,"%"

main()

hey you've got to declare your x variable outside of the main function because anytime the main function is called, the value of x becomes zero. it doesn't matter what count has been made, anytime main() is called, everything will be instantiated to zero before any other thing works.

Hope this helps.
Good luck!

hey you've got to declare your x variable outside of the main function because anytime the main function is called, the value of x becomes zero. it doesn't matter what count has been made, anytime main() is called, everything will be instantiated to zero before any other thing works.

Hope this helps.
Good luck!

I dont get it ? I did this, but gives me an error.

x = 0

def main():
    print "WELCOME TO THE GRADE LEVEL CALCULATOR!"
    print
    
    
    f = 0.0
    s = 0.0
    j = 0.0
    se = 0.0
    nstudents = 0

    while x <= 4:

        print "Please choose the class standing from the following menu:"
        print "     1) Freshman"
        print "     2) Sophomore"
        print "     3) Junior"
        print "     4) Senior"
        print "     5) Quit"
        print

        x = input("Please enter your choice here: ")

        if x > 5 or x < 1:
            print "Invalid choice. Must choose 1 - 5."
            repeat()
            print
        
        elif x == 1:
            f = f + 1
            nstudents = nstudents + 1
    
        
        elif x == 2:
            s = s + 1
            nstudents = nstudents + 1
        elif x == 3:
            j = j + 1
            nstudents = nstudents + 1
        elif x == 4:
            se = se + 1
            nstudents = nstudents + 1
        elif x == 5:

            print "Total number of students entered: ", nstudents

            total = f + s + j + se
            fper = (f / total ) * 100
            sper = (s / total ) * 100
            jper = (j / total ) * 100
            seper = (se / total ) * 100

            print "Freshman:      ", fper,"%"
            print "Sophomores:    ", sper,"%"
            print "Juniors:       ", jper,"%"
            print "Seniors:       ", seper,"%"

main()

I would recommend changing organisation of your code. But if you want keep your style, you must only set the x to zero after error message. No recursive call.

okay. I got this all wrong. First of all I want to be sure what your code is supposed to do is to count up to four times while accepting numbers between one and four from the user. If so, then what are you using as your count? I suggest you declare a different variable to count and then use just x as your input. for example:

def main():

#the part where you declared your variables f,s, j, se add
count = 0

#then all your other codes come in.
#if you want four counts then it's gonna be:
while count < 4:
#if you make it while count <= 4, it's gonna count to 5 this way 0,1,2,3,4
#so now you can have the other lower parts of your code below the while loop remain
#at the end of the while loop, you should increase your count like this
count = count + 1

#you should be good with this

thank you, now it works well :D

sure! I'm glad it works and sorry for the initial confusion.

Have a great day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.