0

i've got an annoying bit of code that i want to have something happen to...

import time
global END
END = 0
def bacteria():
b = int(input("Bacteria number? "))
l = int(input("Limit? "))
i = (float(input("Increase by what % each time? ")))/100+1
h = 0
d = 0
w = 0
while b < l:
    b = b*i
    h = h+1
else:
    while h > 24:
        h = h-24
        d = d+1
    else:
        while d > 7:
            d = d-7
            w = w+1
print("The bacteria took " + str(w) + " weeks, " + str(d) + " days and " + str(h) + " hours.")
def runscript():
    ANSWER = 0
    ANSWER = input("Run what Program? ")
    if ANSWER == "bacteria":
        print(bacteria())
        ANSWER = 0
    if ANSWER == "jimmy":
        print(jimmy())
        ANSWER = 0
    if ANSWER == "STOP" or "stop":
        quit()
while True:
    print(runscript())

So, after the line "if ANSWER == "STOP" or "stop":" i want the script to end; but only if i have entered STOP or stop as the ANSWER, to stop the otherwise infinite loop.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
L0neGamer
  • 275
  • 3
  • 13

2 Answers2

2

Right now, your code is being interpreted like this:

if (ANSWER == "STOP") or ("stop"):

Furthermore, since non-empty strings evaluate to True in Python, this if-statement will always pass because "stop" will always evaluate to True.

To fix the problem, use in:

if ANSWER in ("STOP", "stop"):

or str.lower*:

if ANSWER.lower() == "stop":

*Note: As @gnibbler commented below, if you are on Python 3.x, you should use str.casefold instead of str.lower. It is more unicode compatible.

0

In python the or operator returns true if any of the two operands are non zero.

In this case adding brackets helps clarify exactly what your current logic is:

if((ANSWER == "STOP") or ("stop")):

In python if("stop") will always return True. Because if this the entire condition is always true and quite() will always execute.

In order to fix this you can change your logic to:

if(ANSWER == "STOP") or (ANSWER == "stop"):

or

if ANSWER in ["STOP","stop"]:
RMcG
  • 1,045
  • 7
  • 14