Program Error Help - Python 3.1

Cameron Simpson cs at zip.com.au
Fri Mar 31 19:42:48 EDT 2017


On 31Mar2017 16:12, 2019.cavanaughc at fpsedu.org <2019.cavanaughc at fpsedu.org> wrote:
>Hello I've been coding for about 1 or 2 months and i have encountered a problem with a 'while' statement. It keeps looping even when the statement is false, here is the code. The code is for a game that I'm making that resembles the Oregon Trial.

Obviously the statement (well, "test") isn't false! Let's look:

[...]
>answer8 = ""
>while answer8 != ("1") or answer8 != ("2"):
[...]

Answer8 cannot be both "1" and also "2". Therefore, it will always _not_ be 
equal to at least one of "1" and "2". Therefore this test is always true.

You might better write this:

  while answer8 not in ("1", "2"):

or:

  while not( answer8 == "1" or answer8 == "2" ):

BTW, you need to decide whther you're working in strings or integers. input() 
in Python 3 returns a string. If you want to test against strings, fine.

However, if you want to test against numbers you need to convert the string to 
a number:

  answer8n = int(answer8)

and then test against answer8n.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Python-list mailing list