-1

How do I make the following work (in Python)?

import random
def roll():
    input1 = input("Player1, type 'ROLL' to roll.")
    if (input1 == "ROLL"):
        dice1 = random.randint(1,6)
        dice2 = random.randint(1,6)
        print("You rolled a " + dice1() + " and a " + dice2() ".")

    else:
        pass

roll()

I get:

  File "main.py", line 9
    print("You rolled a " + dice1() + " and a " + dice2() ".")
                                                            ^
SyntaxError: invalid syntax

I also want it to repeat the "Player1, type 'ROLL' to roll." if the input doesn’t equal ROLL.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Blue
  • 15
  • 3

5 Answers5

3

You have an invalid syntax near:

print("You rolled a " + dice1() + " and a " + dice2() ".")

Using string format:

Replace this:

print("You rolled a " + dice1() + " and a " + dice2() ".")

With this:

print("You rolled a {} and {}".format(dice1,dice2))

Note: You can use a while loop to keep taking the user-input unless the input matches with ROLL:

Hence:

import random

def roll():
    while True:
        input1 = input("Player1, type 'ROLL' to roll.")
        if input1 == "ROLL":
            dice1 = random.randint(1,6)
            dice2 = random.randint(1,6)
            print("You rolled a {} and {}".format(dice1, dice2))
            break  
roll()

Output:

Player1, type 'ROLL' to roll.whaaaat?
Player1, type 'ROLL' to roll.okay
Player1, type 'ROLL' to roll.roll
Player1, type 'ROLL' to roll.ROLL
You rolled a 2 and 2
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
DirtyBit
  • 16,151
  • 4
  • 28
  • 54
1

There's a syntax error in the print.

Both int values need to be cast to string using str().

dice1 and dice2 are not functions so we can't write () next to them.

print("You rolled a " + dice1() + " and a " + dice2() ".")

should be

print("You rolled a " + str(dice1) + " and a " + str(dice2) + ".")

Preferrably, you can use the format function that comes with any string.

print("You rolled a {dice1} and a {dice2}.\n".format(
        dice1=dice1,
        dice2=dice2
    )
)
0

The whole function should be:

import random
def roll():
    input1 = input("Player1, type 'ROLL' to roll.")
    if (input1 == "ROLL"):
        dice1 = random.randint(1,6)
        dice2 = random.randint(1,6)
        print("You rolled a %s and a %s." % (dice1,dice2))

    else:
        pass

roll()

If you mean to show the two variables, dice1 and dice2.

Use:

print("You rolled a %s and a %s." % (dice1,dice2))

Instead of:

print("You rolled a " + dice1() + " and a " + dice2() ".")
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
0

You forgot a comma or + in the print statment. You also call variables dice1 dice2 as functions.

import random

def roll():
    input1 = input("Player1, type 'ROLL' to roll. ")
    print(input1)
    if input1 == 'ROLL':
        dice1 = random.randint(1,6)
        dice2 = random.randint(1,6)
        print("You rolled a " + str(dice1) + " and a " + str(dice2) + ".")
    else:
        pass

roll()
Iakovos Belonias
  • 1,049
  • 8
  • 21
-2

Python's random.randint returns an integer, so you don't need to call your dice1 and dice2 variables. Replace this line:

print("You rolled a " + dice1() + " and a " + dice2() ".")

with this:

print("You rolled a " + dice1 + " and a " + dice2 ".")

Reference for Python's random module.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Hrabal
  • 2,158
  • 2
  • 19
  • 28