Python 简单掷骰子程序问题

Python 简单掷骰子程序问题,python,random,while-loop,python-3.3,dice,Python,Random,While Loop,Python 3.3,Dice,我已经创建了一个程序,用户可以选择某个侧面骰子,然后滚动并输出生成的数字,然后询问用户是否想要再次滚动,并使用while循环。我已经写了程序,出于某种原因,它不断重复输入骰子边数字提示,我不知道为什么,下面是代码 import random roll_agn='yes' while roll_agn=='yes': dice=input ('Please choose a 4, 6 or 12 sided dice: ') if dice ==4:

我已经创建了一个程序,用户可以选择某个侧面骰子,然后滚动并输出生成的数字,然后询问用户是否想要再次滚动,并使用while循环。我已经写了程序,出于某种原因,它不断重复输入骰子边数字提示,我不知道为什么,下面是代码

import random

roll_agn='yes'
    while roll_agn=='yes':
        dice=input ('Please choose a 4, 6 or 12 sided dice: ')
        if dice ==4:
                print(random.randint(1,4))
        elif dice ==6:
                print(random.randint(1,6))
        elif dice ==12:
                print(random.randint(1,12))
    else:
        roll_agn=input('that is not 4, 6 or 12, would you like to choose again, please answer yes or no') 
    if roll_agn !='yes':
        print ('ok thanks for playing')

我怀疑这与while循环或缩进有关,但我已经摆弄它大约30分钟了,我无法让它正常工作,因此如果有人能在这里帮助我,我将不胜感激,谢谢

else:roll\u agn=input
上的缩进是这样的,它只在您退出
while
循环后运行-但是
while
循环永远不会结束,直到您运行
else
子句,因此无限循环

这是一个经过清理、结构更好的版本:

# assumes Python 3.x
from random import randint

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))         # if Python 2.x use raw_input instead of input
        except ValueError:
            # not an int
            pass

def get_yn(prompt):
    while True:
        value = input(prompt).strip().lower() # if Python 2.x use raw_input instead of input
        if value in {'y', 'yes'}:
            return True
        elif value in {'n', 'no'}:
            return False

def roll(sides):
    return randint(1, sides)

def main():
    while True:
        sides = get_int("Number of sides on die (4, 6, or 12)? ")

        if sides in {4, 6, 12}:
            print("You rolled a {}".format(roll(sides)))
        else:
            print("U no reed gud?")

        if not get_yn("Play again (y/n)? "):
            print("Thanks for playing!")
            break

if __name__=="__main__":
    main()

else:roll\u agn=input
上的缩进是这样的:它仅在退出
while
循环后运行-但是
while
循环在运行
else
子句之前永远不会结束,因此无限循环

这是一个经过清理、结构更好的版本:

# assumes Python 3.x
from random import randint

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))         # if Python 2.x use raw_input instead of input
        except ValueError:
            # not an int
            pass

def get_yn(prompt):
    while True:
        value = input(prompt).strip().lower() # if Python 2.x use raw_input instead of input
        if value in {'y', 'yes'}:
            return True
        elif value in {'n', 'no'}:
            return False

def roll(sides):
    return randint(1, sides)

def main():
    while True:
        sides = get_int("Number of sides on die (4, 6, or 12)? ")

        if sides in {4, 6, 12}:
            print("You rolled a {}".format(roll(sides)))
        else:
            print("U no reed gud?")

        if not get_yn("Play again (y/n)? "):
            print("Thanks for playing!")
            break

if __name__=="__main__":
    main()

else:roll\u agn=input
上的缩进是这样的:它仅在退出
while
循环后运行-但是
while
循环在运行
else
子句之前永远不会结束,因此无限循环

这是一个经过清理、结构更好的版本:

# assumes Python 3.x
from random import randint

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))         # if Python 2.x use raw_input instead of input
        except ValueError:
            # not an int
            pass

def get_yn(prompt):
    while True:
        value = input(prompt).strip().lower() # if Python 2.x use raw_input instead of input
        if value in {'y', 'yes'}:
            return True
        elif value in {'n', 'no'}:
            return False

def roll(sides):
    return randint(1, sides)

def main():
    while True:
        sides = get_int("Number of sides on die (4, 6, or 12)? ")

        if sides in {4, 6, 12}:
            print("You rolled a {}".format(roll(sides)))
        else:
            print("U no reed gud?")

        if not get_yn("Play again (y/n)? "):
            print("Thanks for playing!")
            break

if __name__=="__main__":
    main()

else:roll\u agn=input
上的缩进是这样的:它仅在退出
while
循环后运行-但是
while
循环在运行
else
子句之前永远不会结束,因此无限循环

这是一个经过清理、结构更好的版本:

# assumes Python 3.x
from random import randint

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))         # if Python 2.x use raw_input instead of input
        except ValueError:
            # not an int
            pass

def get_yn(prompt):
    while True:
        value = input(prompt).strip().lower() # if Python 2.x use raw_input instead of input
        if value in {'y', 'yes'}:
            return True
        elif value in {'n', 'no'}:
            return False

def roll(sides):
    return randint(1, sides)

def main():
    while True:
        sides = get_int("Number of sides on die (4, 6, or 12)? ")

        if sides in {4, 6, 12}:
            print("You rolled a {}".format(roll(sides)))
        else:
            print("U no reed gud?")

        if not get_yn("Play again (y/n)? "):
            print("Thanks for playing!")
            break

if __name__=="__main__":
    main()

看起来if语句有缩进问题。试着将elif与if对齐

if dice ==4:
    print(random.randint(1,4))
elif dice ==6:
    print(random.randint(1,6))
elif dice ==12:
    print(random.randint(1,12))

看起来if语句有缩进问题。试着将elif与if对齐

if dice ==4:
    print(random.randint(1,4))
elif dice ==6:
    print(random.randint(1,6))
elif dice ==12:
    print(random.randint(1,12))

看起来if语句有缩进问题。试着将elif与if对齐

if dice ==4:
    print(random.randint(1,4))
elif dice ==6:
    print(random.randint(1,6))
elif dice ==12:
    print(random.randint(1,12))

看起来if语句有缩进问题。试着将elif与if对齐

if dice ==4:
    print(random.randint(1,4))
elif dice ==6:
    print(random.randint(1,6))
elif dice ==12:
    print(random.randint(1,12))

我尝试过像这样排列缩进,但我没有通过不断重复的“选择骰子边”来解决我的问题。我想休·博思韦尔和汤姆·费内克都发现了这一点。看起来还需要将else语句与if块对齐。看起来您无法在while块中更改roll_agn的值。我还要注意,您可以在randint(1,int(dice))中使用int(dice)并删除elif语句。Hugh Bothwell在roll(sides)中有一个例子。我尝试过像这样排列缩进,但我没有通过不断重复的“选择骰子边”来解决我的问题。我想Hugh Bothwell和Tom Fenech都发现了这一点。看起来还需要将else语句与if块对齐。看起来您无法在while块中更改roll_agn的值。我还要注意,您可以在randint(1,int(dice))中使用int(dice)并删除elif语句。Hugh Bothwell在roll(sides)中有一个例子。我尝试过像这样排列缩进,但我没有通过不断重复的“选择骰子边”来解决我的问题。我想Hugh Bothwell和Tom Fenech都发现了这一点。看起来还需要将else语句与if块对齐。看起来您无法在while块中更改roll_agn的值。我还要注意,您可以在randint(1,int(dice))中使用int(dice)并删除elif语句。Hugh Bothwell在roll(sides)中有一个例子。我尝试过像这样排列缩进,但我没有通过不断重复的“选择骰子边”来解决我的问题。我想Hugh Bothwell和Tom Fenech都发现了这一点。看起来还需要将else语句与if块对齐。看起来您无法在while块中更改roll_agn的值。我还要注意,您可以在randint(1,int(dice))中使用int(dice)并删除elif语句。Hugh Bothwell在roll(sides)中有一个例子。虽然这是一段更好的代码,但这是一个学校项目,我不想交给别人的工作,我只是希望我的工作得到修复,所以如果可能的话,我会非常感激,但感谢你这么做,这给了我下一个项目的想法!虽然这是一段更好的代码,但这是一个学校项目,我不想交给别人的工作,我只想把我的工作修好,所以如果可能的话,我会非常感激,但感谢你这么做,这给了我下一个项目的想法!虽然这是一段更好的代码,但这是一个学校项目,我不想交给别人的工作,我只想把我的工作修好,所以如果可能的话,我会非常感激,但感谢你这么做,这给了我下一个项目的想法!虽然这是一段更好的代码,但这是一个学校项目,我不想交给别人的工作,我只想把我的工作修好,所以如果可能的话,我会非常感激,但感谢你这么做,这给了我下一个项目的想法!