1

I Get an unlimited While Loop In python here's The Code For My Rolling Dice It keeps Rolling The Dice Over and over Again the Code :

#!usr/bin/python
# -*- coding: utf-8 -*-
import random
import time
import sys
print ("")
print ("This is a dice rolling simulator ")
x=raw_input("press Enter to launch the dice ")
def dice():
    print("\nRolling the dice...\n")
    time.sleep(1)
    n=random.randint(1, 6)
    if n == 1:
        print '''
1
            '''
    if n == 2:
        print '''

            '''
    if n == 3:
        print '''
3
            '''
    if n == 4:
        print '''
4
            '''
    if n == 5:
        print '''
5
            '''
    if n == 6:
        print '''
6
            '''

dice()
x=raw_input("press Enter to restart the or type q to quit")
while x!= ("q"):
    dice()
if x== ("q"):
        print ("see you later ")
5
  • You should put the code in the question directly,not through a link to an external resource Commented Aug 23, 2015 at 14:42
  • it seems that you modify x outside of your loop that check x value. Commented Aug 23, 2015 at 14:43
  • @AnandSKumar i'm A Beginner and There's an Error While Putting the Code I'll Try Commented Aug 23, 2015 at 14:44
  • @Baart Thanks I'll Do It For Now It Works Fine I'll Improve It Commented Aug 23, 2015 at 14:46
  • @Hassene-Senpai keep up the good work :) Commented Aug 23, 2015 at 22:59

4 Answers 4

2

You are not reading the input in the while loop. You should read it in the while loop so in each iteration you will be able to change it otherwise it will always do the same calculation.

You loop should look liek this:

x=raw_input("press Enter to restart the or type q to quit")
while x!= ("q"):
    dice()
    x=raw_input("press Enter to restart the or type q to quit")
Sign up to request clarification or add additional context in comments.

Comments

2

you need to get the user input inside the while loop... instead of

x = raw_input("press Enter to restart the or type q to quit")
while x != ("q"):
    dice()

try:

x = raw_input("press Enter to restart the or type q to quit")
while x != ("q"):
    dice()
    x = raw_input("press Enter to restart the or type q to quit")

Comments

1

You'll have to put the raw_input() function inside the while loop at line 40.

x=raw_input("press Enter to restart the or type q to quit")
while x!= ("q"):
    dice()
    x=raw_input("press Enter to restart the or type q to quit")

Comments

0

All the answers that are telling you to duplicate your code are bad. Pythonic solution is

while True:
    dice()
    x = ...
    if x == 'q': break

In this case, you can also just set x='' at the beginning, but generally, there's nothing wrong with a loop that exits somewhere else than at the beginning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.