Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python I'm trying to re loop my code and stop when if statement is right

IDrinkKoolAid

New Coder
[CODE lang="python" title="Five dice all most land on same number"]import random
import time
tries = 0
keep_rolling = "Yes"
while keep_rolling == "Yes":
print("Rolling Dice...")
time.sleep(2)
D1 = random.randint(1,6)
D2 = random.randint(1,6)
D3 = random.randint(1,6)
D4 = random.randint(1,6)
D5 = random.randint(1,6)
print("You have rolled:")

print("1=", D1)
print("2=", D2)
print("3=", D3)
print("4=", D4)
print("5=", D5)
break
if D1 == D2 == D3 == D4 == D5:
print("You Won")
else:
print("Keep Trying")
[/CODE]
 
Hello IDrinkKoolAid,

You must write a short description of what is the problem exactly if you want some help!

You know that the instruction 'break' has the effect of making the current loop ('while' here) stop?

By the way, great name, haha!
 
All you have to do to stop the loop is to change the value of your variable...
Python:
keep_rolling = "Yes"
while keep_rolling == "Yes":
    print("first loop")
    keep_rolling = "No"

If you make your variable keep_rolling = "Something not equal to Yes" then the loop will stop because it only loops while it is equal to "Yes". As @LTomy said, you can use break to get out of a loop, or stop anything under it from executing.

You have a mistake with the indent levels. You are starting a loop, breaking out of your loop immediately, and then doing an if statement outside of the loop code afterwards.

Here's an updated version of your code that I wrote:
Python:
import random
import time

def rolldice(num_of_dice):
    last_value = False
    all_same = True
    for x in range(num_of_dice):
        new_value = random.randint(1,6)
        print(str(x+1)+"=", new_value)
        if x > 0 and new_value != last_value:
            all_same = False
       
    return all_same
 
tries = 0
keep_rolling = True
while keep_rolling is True:
    print("Rolling Dice...")
    time.sleep(1)
    trial = rolldice(5)
    tries += 1
   
    if trial:
        print("You Won After " + str(tries) + " Tries!")
        keep_rolling = False
    else:
        print("Keep Trying...\n")

It gets rid of some of the repetitive code and lets you easily change the script to play with 2 dice, 3 dice, etc. Using only 1 dice for this will likely break the code, but I hope this example of mine gives you some ideas 🙂
 
Last edited:
Back
Top Bottom