Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

When I execute this program inside the function it doesn't work, but when try to run it outside the function it gives me the outcome. I am new to programming, I have no idea why this is happening.

import random

v1 = (random.randint(1,6))

v2 = (random.randint(1,6))

print(v1 + v2)

def roll_dice():

    import random

    v1 = (random.randint(1,6))

    v2 = (random.randint(1,6))

    print(v1 + v2)

    return (roll_dice)

1 Answer

0 votes
by (36.8k points)

You need to call a function in your code after declaring it:

  • Will print the result print(roll_dice())
  • Save to a variable myResult = roll_dice()
  • If it prints itslef then roll_dice()

 Improve your knowledge in data science from scratch using data scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...