Abhay Sharma
Abhay Sharma

Reputation: 15

Not able to find the sum of two choices in python

Here is the code:

num1 = print(random.choice(range(1,7))) 
num2 = print(random.choice(range(1,7)))

I need to add num1 and num2:

total = num1 + num2

Gives an error

Upvotes: 1

Views: 31

Answers (2)

Bill the Lizard
Bill the Lizard

Reputation: 405675

You're getting an error because you're assigning the result of print to a variable, and print returns None, not the value printed. (The error comes later, when you try to add these two NoneType values together.)

Change your code to assign the random values to your variables, then print those and add them together.

num1 = random.choice(range(1,7))
num2 = random.choice(range(1,7))

Upvotes: 2

ncica
ncica

Reputation: 7206

Return Value from print():

It doesn't return any value; returns None.

import random
num1 = random.choice(range(1,7)) # num1 = None
num2 = random.choice(range(1,7)) # num2 = None

total = num1 + num2
print (total)

Upvotes: 0

Related Questions