Python 3中的def函数语法错误

时间:2015-04-17 04:58:05

标签: python function python-3.x syntax-error

我正在教自己编写代码,目前正在制作一个骰子滚轮作为学习项目。此刻,我突然陷入了一个奇怪的障碍。第四个' def'在我的代码中,无论它实际上是什么(我已尝试过几个单独工作)它一直被标记为语法错误。这就是我所拥有的:

import random

def mult():
    print('How many dice would you like to roll?')
    multiplier = input()
    mod()

def mod():
    print('What modifier, if any, would you like to assign?')
    modifier = input()
    result()

def result():
    total = (roll * multiplier) + modifier
    print('Your result is' 
    (str(total)

def menuscreen(): 
    print("Welcome to Jack's dice roller. What kind of die would you like to roll?")
    print("")
    print("A. d2")
    print("B. d4")
    print("C. d6")
    print("D. d8")
    print("E. d10")
    print("F. d12")
    print("G. d20")
    print("H. d100")

def gen():

    menuscreen()

    if input() == 'a' or 'A':
        roll = random.randint(1,2)
        mult()

    if input() == 'b' or 'B':
        roll = random.randint(1,4)
        mult()

    if input() == 'c' or 'C':
        roll = random.randint(1,6)
        mult()

    if input() == 'd' or 'D':
        roll = random.randint(1,8)
        mult()

    if input() == 'e' or 'E':
        roll = random.randint(1,10)
        mult()

    if input() == 'f' or 'F':
        roll = random.randint(1,12)
        mult()

    if input() == 'g' or 'G':
        roll = random.randint(1,20)
        mult()

    if input() == 'h' or 'H':
        roll = random.randint(1,100)
        mult()

def queryque():

    print('Would you care to roll again?')

    if input == 'yes':
        gen()

    if input == 'no':
        end

gen()

3 个答案:

答案 0 :(得分:4)

看起来您在result()定义的末尾缺少两个右括号。你可能想要这样的东西:

def result():
    total = (roll * multiplier) + modifier
    print('Your result is ' + str(total))

答案 1 :(得分:1)

您错过了2个右括号)

(str(total)

请尝试以下方法:

def result():
    total = (roll * multiplier) + modifier
    print 'Your result is' + str(total)

答案 2 :(得分:0)

Pencil Von Radiergummi和AJ已经解决了提示你的问题的语法错误,但正如我在你的问题评论中提到的,你的代码有各种其他问题,主要是在函数中创建的变量是 local < / em>,这意味着它们存在于函数中,并且无法在其他函数中看到。

srekcahrai试图解决问题,但是(目前)他发布的代码存在各种问题(包括print() result()调用中的语法错误功能)。所以这里是srekcahrai答案中代码的修改版本。我修复了代码中的各种错误,但它仍然不完美 - 它确实应该对输入进行错误检查,但至少它会运行。

我通过使用gen()dict字母(可以是大写或小写)转换为边数来缩小choice函数。< / p>

#!/usr/bin/env python

import random

def mod():
    modifier = int(input("What modifier, if any, would you like to assign? "))
    return modifier

def mult():
    multiplier = int(input("How many dice would you like to roll? "))
    return multiplier

def result(roll, multiplier, modifier):
    total = (roll * multiplier) + modifier
    print("Your result is {0}".format(total))

def menuscreen(): 
    print("Welcome to Jack's dice roller.\n")

    print("A. d2")
    print("B. d4")
    print("C. d6")
    print("D. d8")
    print("E. d10")
    print("F. d12")
    print("G. d20")
    print("H. d100")

def gen():
    menuscreen()
    choice = input("What kind of dice would you like to roll? ")
    kinds = {
        "A":2, "B":4, "C":6, "D":8,
        "E":10, "F":12, "G":20, "H":100
    }
    sides = kinds[choice.upper()]
    multiplier = mult()
    modifier = mod()
    roll = random.randint(1, sides)
    result(roll, multiplier, modifier)

def main():
    while True:
        gen()
        if input("Would you care to roll again? ") != "yes":
            print("Goodbye")
            break

if __name__ == "__main__":
    main()

result()函数中的逻辑有点奇怪,但我已为此程序保留了它。实际上,您的程序应该使用循环来将模具滚动指定的次数,每次循环时将结果添加到total。如果您需要帮助,请提出新问题。