-1

I am having some trouble getting break to work in a while-loop.

I have created a function main() which contains three input() regarding global scopes, these three comes directly after each other. I want the program to end immediately if you enter an incorrect value into one of these ìnput() and tried to accomplish that with breakwithout success. So the question is why can't I use break in my while-loop?

Allowed values in my global scopes: Scenario 1 or 2 games should be greater than zero board_size 3, 5 or 7

def main():
    
    while True:    
        board_size = int(input("How big playing surface (3/5/7)? "))
        
        if board_size != 3 or board_size != 5 or board_size != 7:
            break
        
    while True:
        games = int(input("How many games do you want to simulate? "))

        if games <1:
            break

    while True:
        scenario = int(input('What scenario (1/2)? '))

        if scenario != 1 or scenario != 2:
            break

    print_stats(games, scenario, board_size)
ida
  • 61
  • 5
  • 4
    Surely `if board_size != 3 or board_size != 5 or board_size != 7:` will *always* be true. Since the board has a single size, it will fail to have at least two of those three sizes. Use `if board_size not in [3,5,7]:` – John Coleman Jan 10 '21 at 00:56
  • 3
    *"I want the program to end immediately if you enter an incorrect value"* but that's not what `break` does. `break` just ends the current loop, not the whole program. – kaya3 Jan 10 '21 at 00:56
  • 2
    If you want the program to end, use `exit()`. If you want the function to terminate, use `return`; also, 'global scope' doesn't mean what you think it does. It's generally a good idea to use plain English when speaking about programming as a beginner, until you have a good understanding of the jargon. – Grismar Jan 10 '21 at 00:58
  • 1
    You should use ```and```, instead ```or```, in the conditional expression! Alternatively, you can write, for example, ```if board_size not in [3,5,7]: break```. Any way, that will stop only the respective loop ```while```, not the function. If you want to stop the execution of the function, use ```return``` instead of ```break```. – slago Jan 10 '21 at 01:03
  • Thank you @kaya3 for the clarification, then I have previously misunderstood the use of break. – ida Jan 10 '21 at 01:24
  • Thank you @Grismar, I will try to use ``return`` instead. English isn't my main language so i thought global scope was the right term to use at this point, but thank you for the clarification! I will read more about global and local scopes. – ida Jan 10 '21 at 01:27

2 Answers2

2

Your code will always break as it's never possible for board_size to equal 3, 5 or 7:

Also the programme won't "end" upon breaking a while True loop, there are many ways to do this including sys.exit(), raise SystemExit(), exit()... I think you want:

def main():
    
    while True:    
        board_size = int(input("How big playing surface (3/5/7)? "))
        
        if board_size not in [3, 5, 7]:
            raise SystemExit()
        
    while True:
        games = int(input("How many games do you want to simulate? "))

        if games <1:
            raise SystemExit()

    while True:
        scenario = int(input('What scenario (1/2)? '))

        if scenario not in [1, 2]:
            raise SystemExit()

    print_stats(games, scenario, board_size)
GAP2002
  • 870
  • 4
  • 20
  • Thank you! So I used ``True`` incorrectly after what I was looking for by entering ``if board_size != 3 or board_size != 5 or board_size != 7:``etc? – ida Jan 10 '21 at 01:41
1

You can use not logic to check if board_size is equal to 3, 5, or 7. You can use sys.exit() to exit the program, but make sure to import sys.

import sys

def main():
    
    while True:    
        board_size = int(input("How big playing surface (3/5/7)? "))
        
        if not (board_size == 3 or board_size == 5 or board_size == 7):
            sys.exit()
        else:
            break
    while True:
        games = int(input("How many games do you want to simulate? "))

        if games <1:
            sys.exit()
        else:
            break

    while True:
        scenario = int(input('What scenario (1/2)? '))

        if not (scenario == 1 or scenario == 2):
            sys.exit()
        else:
            break

    print(games, scenario, board_size)

main()
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
  • Thank you! Unfortunately ``exit()`` didn't work for me, maybe I need to import something to use it? I will read more about ``exit ()``. – ida Jan 10 '21 at 01:34