Python Forum
While loop won't restart program :(
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: ")) 
Reply
#2
z = int(input("Write 1 to try again: ")) 
is not indented to be inside of the first while loop.
Reply
#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....
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,318 Sep-07-2022, 02:01 AM
Last Post: kaega2
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,740 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  Using a button to kill and restart a script duckredbeard 3 3,245 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  Try-except in while loop: error with closing program Lupin_III 7 2,790 Jul-03-2020, 10:57 AM
Last Post: Lupin_III
  Hotkey to restart code? DannyB 1 2,694 May-20-2020, 02:52 AM
Last Post: michael1789
  Kernel needs to restart ErnestTBass 0 2,325 May-06-2020, 08:37 PM
Last Post: ErnestTBass
  How to restart Python after input change ozstar 3 2,321 Apr-29-2020, 03:16 AM
Last Post: ozstar
  Restart Error when using code from lesson book Kathleen57 2 2,213 Mar-13-2020, 09:18 PM
Last Post: Kathleen57
  please help me in loop i am trying to run this program in raspberry pi by sending msg saifullahbhutta 2 2,296 Jan-27-2020, 02:32 PM
Last Post: saifullahbhutta
  How to rerun the program in a loop with different input? bharaths 3 3,712 Nov-23-2018, 09:36 AM
Last Post: bharaths

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020