Problems with looping

This is the place for queries that don't fit in any of the other categories.

Problems with looping

Postby billygo » Tue Apr 01, 2014 11:41 am

Hey guys just working on some code (same code as other thread)

Code: Select all
finished = False
while not finished:
#
    name=raw_input("Please enter your name: ")
    print("Hi " + name + "! and welcome to the virtual game of car cricket")
    #
    name2=raw_input("Please enter the second players name: ")
   
    if (name2 == name):
        print("Players cannot have the same name")
        finished = True
    else:
        print("Hi " + name2 + "! and welcome to the virtual game of car cricket")
    #


after entering the same name it doesn't end the script, it just continues as usual (after printing players cannot have the same name).

all I'm trying to do is have it so if the names are the same the program stops instead of continuing, If anyone can help me it'd be greatly appreciated, cheers heaps.
billygo
 
Posts: 4
Joined: Wed Mar 26, 2014 9:50 am

Re: Problems with looping

Postby billygo » Tue Apr 01, 2014 11:48 am

just learned pythons quit() so i worked it out, but if anyone has a solution as to why my original code didn't work id love to hear it
billygo
 
Posts: 4
Joined: Wed Mar 26, 2014 9:50 am

Re: Problems with looping

Postby stranac » Tue Apr 01, 2014 12:10 pm

Why do you think your program should have stopped?
If you don't tell it to, how would it know?

As for quit, you should never be using it(it's just an implementation detail).
If you really need to exit the program like that, use sys.exit().

Do note that, in well structured programs, there is usually a better way, such as e.g. exiting the main function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Problems with looping

Postby pnelsonsr » Tue Apr 01, 2014 6:29 pm

Code: Select all
if (name2 == name):
        print("Players cannot have the same name")
        finished = True
    else:
        print("Hi " + name2 + "! and welcome to the virtual game of car cricket")

I don't think this is what you really mean to happen... I think you want to loop until finished (and finished means you have 2 unique names) otherwise keep looping, so maybe move your finished statement to where this is really finished as in:

Code: Select all
if (name2 == name):
        print("Players cannot have the same name")
    else:
        print("Hi " + name2 + "! and welcome to the virtual game of car cricket")
        finished = True
pnelsonsr
 
Posts: 28
Joined: Thu Dec 12, 2013 12:48 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest