简体   繁体   中英

I can't get the first if statement to work in the loop.

I'm trying to create this simple code for a class project. When I typed in the first if statement, my equations no longer worked but the if statement to trigger the "you are done" and break statements work. Taking out the first if statement causes my equations to work but i can't end the program. Is there any way to get around this?

   # input [4, 6, 12, 9, 1, 2]
    S = 0
    C = 0
    X = 999
    while True:
        Xstring = raw_input("Please Enter a Number: ")
        if Xstring == int(X):
            X = int(Xstring)
            S = S + X
            C = C + 1
            print C
            if C > 0:
                A = S / C
                print A
        elif Xstring == (""):
            print str('You are done.')
            break

You have it backwards. It should be if int(Xstring) == X . You also don't need to do X = int(Xstring) because the if statement already established that they're equal. Also, C > 0 will always be True because it starts at 0 and only goes up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM