-1

What I want to do is have Python generate a number between 1 and 6, then add 6 to that number which I have got it to do and provide me with a result, however what I can't seem to figure out is how to make that value able to be called so it can go up and down during the game, here is what I have so far:

import random
import time
Name = input("Before we get started lets get your character created, tell me what is your name?") 
print()
print ("Welcome ",Name," to the adventure of a lifetime. The next thing you will need to do is sort out your character stats. This will require dice rolls which we will do within the game.")
print()

def skillroll():
    skillroll=""

    while skillroll !="Y" and skillroll != "N":
        skillroll = input("First we need to roll for your skill, would you like me to roll? Y/N")

        if skillroll=="Y":
            print("Rolling the dice..")
            skill=random.randint(1,6)
            time.sleep(2)
            print("I rolled a", skill, " I will now add 6 to this so your skill is", skill+6)
            skill=skill+6
            print()

    return skillroll

skillroll()

I just cant see how to get that final answer out so I can use it during the game-play.

A friend of mine sent me this to help https://github.com/protocol7/python-koans/blob/master/python%202/koans/about_classes.py

But I just cant see how this relates and every answer I found on Stackoverflow is for different languages.

Kyoujin
  • 315
  • 1
  • 5
  • 19
  • For your case, defining and using `skill` as a [global variable](http://www.python-course.eu/global_vs_local_variables.php) should be enough. – zwer Jul 18 '17 at 21:41
  • I dont fully understand. Do you want this number to be available everywhere in your code? – Joshua Nixon Jul 18 '17 at 21:41
  • 1. you cannot have function and variable having this same names, in python every function is also an object so You are overriding it every time – Take_Care_ Jul 18 '17 at 21:44
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. Also, you've provided far more code than needed to demonstrate your problem; see also [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Prune Jul 18 '17 at 21:44

1 Answers1

1

Simply use:

import random
random.randrange(1, 7)

to get any number between 1 and 6.

Your code becomes:

import random
import time

def skillroll():
    skillroll=""
    while skillroll !="Y" and skillroll != "N":
        skillroll = input("First we need to roll for your skill, would you like me to roll? Y/N")
        if skillroll=="Y":
            print("Rolling the dice..")
            skill = random.randrange(1, 7)
            time.sleep(2)
            print("I rolled a ", skill, ". I will now add 6 to this so your skill is", skill+6, "\n")
            skill=skill+6
            return skillroll # What if N??

Name = input("Before we get started lets get your character created, tell me 
what is your name?") 
print ("\nWelcome ",Name," to the adventure of a lifetime. The next thing you 
will need to do is sort out your character stats. This will require dice 
rolls which we will do within the game.\n")
skillroll()
Fabien
  • 4,862
  • 2
  • 19
  • 33