Skip to main content
 首页 » 编程设计

python之在 Python 中无限循环

2025年05月04日32powertoolsteam

我在 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("\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")