Python中的无限循环
我在python中得到了一个无限的While循环这是我掷骰子的代码 它一次又一次地掷骰子 守则:Python中的无限循环,python,while-loop,Python,While Loop,我在python中得到了一个无限的While循环这是我掷骰子的代码 它一次又一次地掷骰子 守则: #!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
#!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 ")
您没有读取while循环中的输入。您应该在while循环中读取它,这样在每次迭代中您都可以更改它,否则它将始终执行相同的计算 你应该看看这个:
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")
您没有读取while循环中的输入。您应该在while循环中读取它,这样在每次迭代中您都可以更改它,否则它将始终执行相同的计算 你应该看看这个:
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")
您需要在while循环中获取用户输入。。。而不是
x = raw_input("press Enter to restart the or type q to quit")
while x != ("q"):
dice()
尝试:
您需要在while循环中获取用户输入。。。而不是
x = raw_input("press Enter to restart the or type q to quit")
while x != ("q"):
dice()
尝试:
您必须将原始输入函数放入第40行的while循环中
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")
您必须将原始输入函数放入第40行的while循环中
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")
所有告诉您复制代码的答案都是错误的。Pythonic解决方案是
while True:
dice()
x = ...
if x == 'q': break
在这种情况下,您也可以在开始时设置x=,但通常情况下,在开始时以外的地方存在的循环没有什么问题。所有告诉您复制代码的答案都是错误的。Pythonic解决方案是
while True:
dice()
x = ...
if x == 'q': break
在这种情况下,您也可以在开始时设置x=,但通常情况下,在开始时以外的地方存在的循环没有问题。您应该直接将代码放入问题中,不是通过指向外部资源的链接,似乎您在检查x值的循环之外修改了x。@AnandSKumar我是初学者,在输入代码时出错,我将Try@Baart谢谢,我现在就做,很好,我会改进的It@Hassene-Senpai继续做好工作:你应该直接将代码放入问题中,不是通过指向外部资源的链接,似乎您在检查x值的循环之外修改了x。@AnandSKumar我是初学者,在输入代码时出错,我将Try@Baart谢谢,我现在就做,很好,我会改进的It@Hassene-森派继续做好工作: