This is an Assignment I have to do, I did everything but just don't know how to loop it. The part where it says "You need to pick a number from 1-3" I want it to loop even if the user entered a wrong key. Because right now, if the user enters a option that is not a choice, it will exit the whole program.

house= ["computer", "laptop", "tv", "nintendo DS", "table", "Empty"]
print "items in my house are:"
print house

print "Pick an item to fill in the empty space: \n1 - Lamp\n2 - bed\n3 - NDS\n\n"

choice=raw_input("Type a number and press enter: ")
print


if choice =="1":
    house[5] = "lamp"
    print "Items in my house are now:"
    print house

elif choice =="2":
    house[5] = "bed"
    print "Items in my house are now:"
    print house

elif choice =="3":
    house[5] = "NDS"
    print "Items in my house are now: "
    print house

else:
    print "\nYou need to pick a number from 1-3"

raw_input("\n\nPress the enter key to exit.")

You can do it this way ...

house= ["computer", "laptop", "tv", "nintendo DS", "table", "Empty"]

print "items in my house are:"
print house

menu = """\
Pick an item to fill in the empty space:
1 - Lamp
2 - bed
3 - NDS
q - to quit
"""

print menu

while True:
    print
    choice = raw_input("Type a number and press enter: ")

    if choice =="1":
        house[5] = "lamp"
        print "Items in my house are now:"
        print house

    elif choice =="2":
        house[5] = "bed"
        print "Items in my house are now:"
        print house

    elif choice =="3":
        house[5] = "NDS"
        print "Items in my house are now: "
        print house

    elif choice.lower() == 'q':
        break

    else:
        print "\nYou need to pick a number from 1-3 \a"

raw_input("\n\nPress the enter key to exit. ")

You can do it this way ...

house= ["computer", "laptop", "tv", "nintendo DS", "table", "Empty"]

print "items in my house are:"
print house

menu = """\
Pick an item to fill in the empty space:
1 - Lamp
2 - bed
3 - NDS
q - to quit
"""

print menu

while True:
    print
    choice = raw_input("Type a number and press enter: ")

    if choice =="1":
        house[5] = "lamp"
        print "Items in my house are now:"
        print house

    elif choice =="2":
        house[5] = "bed"
        print "Items in my house are now:"
        print house

    elif choice =="3":
        house[5] = "NDS"
        print "Items in my house are now: "
        print house

    elif choice.lower() == 'q':
        break

    else:
        print "\nYou need to pick a number from 1-3 \a"

raw_input("\n\nPress the enter key to exit. ")

I was doing that way at first, but the teacher wanted to look like this: http://i44.tinypic.com/nf5hv.jpg
- To not have a 'quit' option.
- and if the user enters a invalid key, it will repeat starting from "Pick an item to fill in the empty space" part.

Can you TRY to use some of what you got?

If its not quite right, change it a little.

Try starting the loop like this:

while house[5] == "Empty":
    print "Pick an item to fill in the empty space: \n1 - Lamp\n2 - bed\n3 - NDS\n\n"

You will have to indent the other lines that should be in the loop.

I was jus wondering if there is a WAY to just add a simple code, and it will loop. Never knew adding a loop would need to change the program that much. o_o And I need the program to look like that link i posted earlier (^ up above) So i try not to change it to much to look different.

Can you TRY to use some of what you got?

If its not quite right, change it a little.

Try starting the loop like this:

while house[5] == "Empty":
    print "Pick an item to fill in the empty space: \n1 - Lamp\n2 - bed\n3 - NDS\n\n"

You will have to indent the other lines that should be in the loop.

I tried what you said, It just keeps looping non-stop, it doesn't even give a chance for the user to enter a response. Or i did it wrong?

I specifically did NOT post fully working code...it is YOUR assignment.

You needed to indent more of the code that followed.

Specifically, the input and the tests.

All of this should have been in the loop too:

choice=raw_input("Type a number and press enter: ")
print


if choice =="1":
    house[5] = "lamp"
    print "Items in my house are now:"
    print house

elif choice =="2":
    house[5] = "bed"
    print "Items in my house are now:"
    print house

elif choice =="3":
    house[5] = "NDS"
    print "Items in my house are now: "
    print house

else:
    print "\nYou need to pick a number from 1-3"

I did, I didn't Just copy and paste the two lines of code. I have common sense. -.-. And that is what I did, i copied everything else including that example you said. Maybe there was a slight mistake, I'll go recheck it. Thank you for the help.

It seems to work right in my sample code. If you still can't make it work, post your code (and your output if possible) and we can look it over again.

It seems to work right in my sample code. If you still can't make it work, post your code (and your output if possible) and we can look it over again.

house= ["computer", "laptop", "tv", "nintendo DS", "table", "Empty"]
print "items in my house are:"
print house
    
while house[5] == "Empty":
    print "Pick an item to fill in the empty space: \n1 - Lamp\n2 - bed\n3 - NDS\n\n"

choice=raw_input("Type a number and press enter: ")
print


if choice =="1":
    house[5] = "lamp"
    print "Items in my house are now:"
    print house

elif choice =="2":
    house[5] = "bed"
    print "Items in my house are now:"
    print house

elif choice =="3":
    house[5] = "NDS"
    print "Items in my house are now: "
    print house

else:
    print "\nYou need to pick a number from 1-3"

I kept everything the same, accept added the two lines of codes you suggested. But it just loops non-stop.

Please, when posting python code, use code=python tags

As I said in my previous messages, you needed to indent more lines to make them part of the loop.

house= ["computer", "laptop", "tv", "nintendo DS", "table", "Empty"]
print "items in my house are:"
print house
    
while house[5] == "Empty":
    print "Pick an item to fill in the empty space: \n1 - Lamp\n2 - bed\n3 - NDS\n\n"

    choice=raw_input("Type a number and press enter: ")
    print


    if choice =="1":
        house[5] = "lamp"
        print "Items in my house are now:"
        print house

    elif choice =="2":
        house[5] = "bed"
        print "Items in my house are now:"
        print house

    elif choice =="3":
        house[5] = "NDS"
        print "Items in my house are now: "
        print house

    else:
        print "\nYou need to pick a number from 1-3"

In python, the indenting is CRITICAL to the program structure, so much so that having two items indented at different levels is actually a compiler error: (** This code will NOT compile **)

sum = 0
for x in range(5):
    sum += x
        print x, "new sum", sum

It is actually one of the several features of python that I love. Either learn to love it, learn to live with it, or move on to another language.

Syntactical whitespace is one of the main complaints I hear about Python, and it is one of the reasons I like Python so much. I really like knowing that when I read Murtan's code it is going to look like mine.

oh, now i get it. I always miss out on common mistakes like that, or forget it. :S Okay, thanks alot. :)

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.