简体   繁体   中英

def function syntax error in Python 3

I'm in the process of teaching myself to code and am currently working on a dice roller as a learning project. I'm runnng into a bit of an odd snag at the moment the moment. The fourth 'def' in my code, no matter what it actually is (I've tried with several that all work individually) it keeps getting flagged as a syntax error. Here's what I have:

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()

It looks like you're missing two closing parentheses at the end of your result() definition. You probably want something like:

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

You're missing 2 closing parentheses ) :

(str(total)

Try the following instead:

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

Pencil Von Radiergummi and AJ have addressed the syntax error that prompted your question, but as I mentioned in my comment to your question your code has various other problems, the main one being that variables created in functions are local , which means they only exist within the function and can't be seen in other functions.

srekcahrai attempted to address that problem, but there are (currently) various issues with the code he posted (including a syntax error in the print() call in the result() function). So here's a modified version of the code in srekcahrai's answer. I've fixed various errors in the code but it's still not perfect - it really should have error checking on the inputs, but at least it runs.

I've condensed the gen() function by using a dict to convert the choice letter (which can be in upper or lower case) to the number of sides.

#!/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()

The logic in your result() function is a little odd, but I've retained it for this program. Realistically, your program should use a loop to roll the die the specified number of times, adding the results to total each time through the loop. If you need help doing this, please ask a new question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM