2

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()
7
  • First, don't try to post Python as code snippets; that only works for JavaScript. I'll edit that. Commented Apr 17, 2015 at 5:01
  • 4
    For future reference, when you encounter an error you will make potential responders lives' much easier by including the stack trace since it will show exactly where the error is. Commented Apr 17, 2015 at 5:03
  • 4
    As a general rule, in Python, when you get a SyntaxError on a line that looks perfectly valid, look upward to see if you missing a ) (or, occasionally, a ] or }). That's a very common error—the missing ) means it's trying to interpret the next line as part of the expression in the previous line, and you can't throw, say, a def in the middle of an expression. You may also want to consider getting a better editor that can assist you with balancing parens; they're useful for almost all programming languages. Commented Apr 17, 2015 at 5:05
  • Sorry, as you can tell I haven't done a great deal of coding before, pardon my ignorance. Commented Apr 17, 2015 at 5:06
  • I tried to find a good dup to link this to, but it looks like we're now closing them as typos instead. That's a shame, because it would be nice to have an explanation somewhere… but I guess I'll go along with it. Commented Apr 17, 2015 at 5:06

3 Answers 3

3

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Yep that was it, I didn' have a closing parthesis on the print command, just left the string open. Thank you :)
No problem. Welcome to Python!
2

You're missing 2 closing parentheses ):

(str(total)

Try the following instead:

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

Comments

1

Pencil Von Radiergummi and A.J 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.