1

I am writing a program with python to simulate flipping a coin or rolling dice. The coin and dice use while loops to give the user the option to roll or flip again, an example is:

def d4():
    d4ing = True
    while d4ing:        
        print(random.randint(1,4))
        done = input("""would you like to roll again?  Type y to roll again,
type d to roll a dice, or type anything else to exit:""")
        if done == "y":
            continue
        elif done == "d":
            break
        else:
            print("thank you for using my coin/dice simulator")
            sys.exit("goodbye")

The problem I am having is that I would like to take every line starting from done and make it into it's own function that I can just insert into every function rather than typing the whole thing out again and again, like this.

def d4ing():
    d4ing = True
    while d4ing:
        print(random.randint(1,4))
        rerolling()

def rerolling(): 
    done = input("""would you like to roll again? Type y to roll again, type d to roll a dice, or type anything else to exit:""") 
    if done == "y": 
        continue 
    elif done == "d": 
        break else: 
    print("thank you for using my coin/dice simulator")     
    sys.exit("goodbye") 

The error message I am getting:

SyntaxError: 'continue' not properly in loop
6
  • So, what prevents you from doing what you would like? Be as specific as possible. Commented Feb 21, 2018 at 5:03
  • I tried to create a function called rerolling() and it comes up with a syntax error that continue and break are not properly in a loop. Thank you for help editing the question, I'm new to the site. Commented Feb 21, 2018 at 5:06
  • 1
    Please show us what you did and specifically include the complete error message. Commented Feb 21, 2018 at 5:09
  • You cannot break if you are not inside the loop. Commented Feb 21, 2018 at 5:15
  • def rerolling(): done = input("""would you like to roll again? Type y to roll again, type d to roll a dice, or type anything else to exit:""") if done == "y": continue elif done == "d": break else: print("thank you for using my coin/dice simulator") sys.exit("goodbye") The error message I am getting is: continue ^ SyntaxError: 'continue' not properly in loop Commented Feb 21, 2018 at 5:15

1 Answer 1

3

A break or a continue must be in a loop in its current scope. You cannot break a loop in the above scope from inside a function. Here is a general example of what raises the SyntaxError: 'break' outside loop error. The same holds for continue.

def break_up():
    break # This is a syntax error

while True:
    break_up()

Although, this is not a problem, since you can make the function return a value and conditionally break in the upper scope.

In your specific example though, you can also return whether or not you want to reroll by assigning the return value to d4ing.

def d4():
    d4ing = True
    while d4ing:        
        print(random.randint(1,4))
        d4ing = rerolling()

def rerolling():
    done = input("Would you like to roll again?")
    if done == "y":
        return True
    elif done == "d":
        return False
    else:
        print("thank you for using my coin/dice simulator")
        sys.exit("goodbye")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.