oso9817 oso9817 - 1 year ago 67
Python Question

Loop not exiting if roll_more == 'no'

I tried making a simple dice rolling program, and I can get the dice to 'roll' but I can't stop it, it only keeps if you enter anything. I tried to add it so if you say 'no' or 'n' it will return and go to my other functions as usual. I say return because continuing to my other code is important. Thank you.

import random
min = 1
def rolling6():
roll_more = "yes"
while roll_more == "yes" or roll_more =="y":
max = 6
print("Rolling the dices...")
print("The values are...")
print(random.randint(min, max))
print(random.randint(min, max))
roll = input("Roll the die again? ")

if roll_more == "no" or roll_more == "n":
#roll_again = "yes"
#while roll_again == "yes" or roll_again =="y":
# print("Rolling the dices...")
# print("The values are...")
# print(random.randint(min, 6))
# print(random.randint(min, 6))
# roll_again = input("Roll the die again? ")

#if roll_again == "no" or roll_again == "n":
# return

x = input("Use six sided die? ")
while x == "yes" or x =="y":

Answer Source

You are writing to roll instead of roll_more, so you are always checking against the constant "yes".

Furthermore, your x and roll_more variables don't hold the values you expect. First, you input "yes" to enter the loop, for rolling6 to ever be called. Then, you enter the rolling6 loop. When you exit out of that by inputing "no", you exit out to the outer loop, read x which still has the value "yes" (since you never overwrote it in any place), which means you don't break out of that loop, and that you enter back into rolling6 again.

You probably want to change roll to roll_more and change while x == "yes" or x =="y": to if x == "yes" or x =="y":.

Also, your if roll_more == "no" or roll_more == "n": return is redundant, since there's no code after that statement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download