试图在python中制作骰子滚动脚本

时间:2018-09-09 17:57:48

标签: python

第15行的打印int(rollval)出现语法模糊的错误。

from random import randint
roll == 0
def diceroll(roll):
    def dicenum(userdicenum):
        userdicenum = int(raw_input("How many dice would you like to roll?"))
        return userdicenum

    def dicesides(userdiceside):
        userdiceside = int(raw_input("How many sides for each die?"))
        return userdiceside


    for rollval in range(1,userdicenum):
        rollval = randint(1,userdiceside)
        print int(rollval)
        roll = roll + rollval
        return roll
    print roll

2 个答案:

答案 0 :(得分:2)

from random import randint
def diceroll():
    def dicenum():
        userdicenum = int(input("How many dice would you like to roll?"))
        return userdicenum

    def dicesides():
        userdiceside = int(input("How many sides for each die?"))
        return userdiceside

    roll = 0
    dicesida = dicesides() # so you dont have to re type it =)
    for rollval in range(dicenum()):
        rollval = randint(1,dicesida)
        print(int(rollval))
        roll = roll + rollval
    return roll
print(diceroll())

这是你想要的吗?

答案 1 :(得分:0)

Python3和Python2之间的一个区别是在Python3中,print语句在Python3中是一个函数,而在Python2中是一个关键字。它是一个函数的事实意味着您必须像使用其他函数一样使用它,方法是将参数放在括号内。

使用print(int(rollval))

您还应该看看第二行。 roll == 0应该是roll = 0。并且如注释中所述,您也不应在Python3中使用raw_input。使用input