我的骰子滚动计划有什么问题?

时间:2016-11-20 17:54:17

标签: python dice

我是python的新手,我正在尝试制作一个基本的骰子滚动程序但是当我运行程序时,它从不显示骰子图片。图片是看起来像死的东西,我不是想导入图片。这是我的代码

leaveprogram = 0
from random import randint
uniform = 0
random = 0
while leaveprogram != "q":
    print ("Dice Rolling Program")
    print ("press enter to roll")
    input()
    number = randint(1, 6)
if number == 1:
    print("[-----------]")
    print("[           ]") 
    print("[     o     ]")
    print("[           ]")
    print("[-----------]")
    leaveprogram=input()
if number == 2:
    print ("[-----------]")
    print ("[           ]")
    print ("[    o o    ]")
    print ("[           ]")
    print ("[-----------]")
    leaveprogram=input()
if number == 3:
    print ("[-----------]")
    print ("[    o o    ]")
    print ("[     o     ]")
    print ("[           ]")
    print ("[-----------]")
    leaveprogram=input()
if number == 4:
    print ("[-----------]")
    print ("[    o o    ]")
    print ("[    o o    ]")
    print ("[           ]")
    print ("[-----------]")
    leaveprogram=input()
if number == 5:
    print ("[-----------]")
    print ("[  o     o  ]")
    print ("[     o     ]")
    print ("[  o     o  ]")
    print ("[-----------]")
    leaveprogram=input()
if number == 6:
    print ("[-----------]")
    print ("[  o     o  ]")
    print ("[  o     o  ]")
    print ("[  o     o  ]")
    print ("[-----------]")
    leaveprogram=input()

3 个答案:

答案 0 :(得分:0)

在python中,缩进规定了循环的范围或条件。 你的if语句应该缩进,如下所示:

leaveprogram = 0
from random import randint
uniform = 0
random = 0
while leaveprogram != "q":
    print ("Dice Rolling Program")
    print ("press enter to roll")
    input()
    number = randint(1, 6)
    if number == 1:
        print("[-----------]")
        print("[           ]") 
        print("[     o     ]")
        print("[           ]")
        print("[-----------]")
        leaveprogram=input()
. . .

答案 1 :(得分:0)

您需要更正缩进,而且您不需要在leaveprogram条件下获得if的输入。

from random import randint
leaveprogram = 0
uniform = 0
random = 0
while leaveprogram != "q":
    print ("Dice Rolling Program")
    print ("press enter to roll")
    leaveprogram=input()
    number = randint(1, 6)
    if number == 1:
        print("[-----------]")
        print("[           ]") 
        print("[     o     ]")
        print("[           ]")
        print("[-----------]")
    if number == 2:
        print ("[-----------]")
        print ("[           ]")
        print ("[    o o    ]")
        print ("[           ]")
        print ("[-----------]")
    if number == 3:
        print ("[-----------]")
        print ("[    o o    ]")
        print ("[     o     ]")
        print ("[           ]")
        print ("[-----------]")
    if number == 4:
        print ("[-----------]")
        print ("[    o o    ]")
        print ("[    o o    ]")
        print ("[           ]")
        print ("[-----------]")
    if number == 5:
        print ("[-----------]")
        print ("[  o     o  ]")
        print ("[     o     ]")
        print ("[  o     o  ]")
        print ("[-----------]")
    if number == 6:
        print ("[-----------]")
        print ("[  o     o  ]")
        print ("[  o     o  ]")
        print ("[  o     o  ]")
        print ("[-----------]")

答案 2 :(得分:0)

除了缩进问题之外,您可能希望通过创建一个字符串数组来重构代码,每个字符串一个,并用一行代码替换if语句的级联。一种方法是利用Python三引号字符串文字以及split()方法:

diceString = """
***************
[-----------]
[           ] 
[     o     ]
[           ]
[-----------]
***************
[-----------]
[           ]
[    o o    ]
[           ]
[-----------]
***************
[-----------]
[    o o    ]
[     o     ]
[           ]
[-----------]
***************
[-----------]
[    o o    ]
[    o o    ]
[           ]
[-----------]
***************
[-----------]
[  o     o  ]
[     o     ]
[  o     o  ]
[-----------]
***************
[-----------]
[  o     o  ]
[  o     o  ]
[  o     o  ]
[-----------]"""

die = diceString.split("\n***************\n")

这使得die[0]成为空字符串具有很好的副作用,因此您感兴趣的字符串从索引1开始并始终对应于骰子上的数字。如果你走这条路线,那么if语句的整个级联可以用单行代替:

print(die[number])