I tried to make a way to let the user choose to which piece of the program it would go, i first tried for but later went and used if, this is the code:

while True:
    whichcode = 1
    #set the variable for the first time.
while True:

    if whichcode == 1:
        print "code nr one"
        print "where to go?"
        x = raw_input("1 or 2 or 3? ")
        if x == 1:
            whichcode = 1
        if x == 2:
            whichcode = 2
        if x == 3:              #let the user choose
            whichcode = 3       #which piece of code it should go to.

    if whichcode == 2:
        print "code nr 2"
        print "where to go?"
        x = raw_input("1 or 2 or 3:? ")
        if x == 1:
            whichcode = 1
        if x == 2:
            whichcode = 2       #let user choose again.
        if x == 3:
            whichcode = 3

    if whichcode == 3:
        print "code nr 3"
        print "where to go?"
        x = raw_input("1 or 2 or 3:? ")
        if x == 1:
            whichcode = 1   
        if x == 2:
            whichcode = 2       #and again.
        if x == 3:
            whichcode = 3

(I now get zero syntax errors, so thats not a problem anymore.)
but when i run it does nothing, i can type, but it stays at typing, it doest print anything, and when i use ctrl+c to get out if the loop is says that is has stayed at line 1, the first while True. does anyone know why?

(this is what is says after i ctrl + c out of the loop:)

^CTraceback (most recent call last):
  File "forloops.py", line 1, in <module>
    while True:
KeyboardInterrupt

Recommended Answers

All 3 Replies

The while True loop runs forever unless it contains a break or return statement, or an exception is raised. It means that your code repeats indefinitely the statement whichcode = 1. This is probably not what you meant. Why do you need the while loop ?

thanks, thats at least fixed, i seem to have overlooked the part where it said that, i thought it would just run to the next part (if whichcode == 1) but turns out it doesnt. thanks a lot!

i worked with the code some more and came with this:

q = 1
    #set the variable for the first time.
while True:

    if q == 1:
        print "code nr 1"
        print "where to go?"
        x = raw_input("1 or 2 or 3? ")
        if x == "1":
            q = 1
        elif x == "2":
            q = 2
        elif x == "3":              #let the user choose
            q = 3       #which piece of code it should go to.
        else:
            print "choose a number you dumb!"

    elif q == 2:
        print "code nr 2"
        print "where to go?"
        x = raw_input("1 or 2 or 3:? ")
        if x == "1":
            q = 1
        elif x == "2":
            q = 2       #let user choose again.
        elif x == "3":
            q = 3
        else:
            print "choose a number you dumb!"

    elif q == 3:
        print "code nr 3"
        print "where to go?"
        x = raw_input("1 or 2 or 3:? ")
        if x == "1":
            q = 1   
        elif x == "2":
            q = 2       #and again.
        elif x == "3":
            q = 3
        else:
            print "choose a number you dumb!"

it wont actually quit the programm yet, but that wasnt the idea, thanks for helping!

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.