Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 While loop won't restart program :(
#1
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: ")) 
Quote
#2
z = int(input("Write 1 to try again: ")) 
is not indented to be inside of the first while loop.
Quote
#3
(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....
Quote
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
(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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to rerun the program in a loop with different input? bharaths 3 473 Nov-23-2018, 09:36 AM
Last Post: bharaths
  Nested while loop in pyramid program. raj55 2 1,599 May-04-2018, 10:05 AM
Last Post: ThiefOfTime
  How To Make Script Restart? ghost0fkarma 1 821 Dec-07-2017, 01:30 PM
Last Post: buran
  WHILE LOOP in my program kurokszxcky 1 1,211 Oct-02-2017, 03:45 PM
Last Post: ichabod801
  (beginner) How do I insert a restart game option? hshe2 1 1,948 Jan-18-2017, 11:47 AM
Last Post: j.crater

Forum Jump:


Users browsing this thread: 1 Guest(s)