Python Forum

Full Version: While loop won't restart program :(
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I am very new to python, and I am trying to make a simple dice roll simulator. But when I try to loop the program it just doesn't do anything, I've done loops before and am pretty familiar with them (for a noob), but it just doesn't seem to work.

This is the code:

import random

z=1

while z==1:

    y = 1

    n = int(input("How many dice do you want to roll?: "))

    counter1 = 0
    counter2 = 0
    counter3 = 0
    counter4 = 0
    counter5 = 0
    counter6 = 0

    for x in range (1,n+1):
        t = random.randint(1,6)

    while y <= n:
        if t == 1:
          counter1 += 1
        elif t == 2:
          counter2 += 1
        elif t == 3:
          counter3 += 1
        elif t == 4:
          counter4 += 1
        elif t == 5:
          counter5 += 1
        elif t == 6:
          counter6 += 1
                    
        for x in range (1,n+1):
            t = random.randint(1,6)

        y += 1

    print("1: ", counter1, " times, 2: ", counter2, " times, 3: ", counter3, " times 4: ", counter4, " times, 5: ", counter5, " times, 6: ", counter6, " times.")
    z=2
        

z = int(input("Write 1 to try again: ")) 
z = int(input("Write 1 to try again: ")) 
is not indented to be inside of the first while loop.
(Apr-04-2019, 06:59 PM)Yoriz Wrote: [ -> ]
z = int(input("Write 1 to try again: ")) 
is not indented to be inside of the first while loop.

I'm not quite sure I catch what you are saying. It isn't in the first while loop what I can see....
You need to indent the line Yoriz pointed out. The line before that, you set z to 2. That ends the loop, because it only repeats if z is one. Since the last line is not in the loop, you don't get a chance (in the loop) to stay in the loop.
(Apr-04-2019, 07:26 PM)ichabod801 Wrote: [ -> ]You need to indent the line Yoriz pointed out. The line before that, you set z to 2. That ends the loop, because it only repeats if z is one. Since the last line is not in the loop, you don't get a chance (in the loop) to stay in the loop.

Oh I see! Thank you guys! :D