0
     import random

     characterNameOne=str(input("Please input first character's name"))
     characterNameTwo=str(input("Please input second character's name"))

     print("The characters have 2 attributes : Strength and Skill")

     dieOne = random.randint(1,4)
     dieTwo = random.randint(1,12)

     print ("A 12 and 4 sided dice are rolled")

     print("Each character is set to 10")

     characterOneStrength = 10
     characterOneSkill = 10
     characterTwoStrength = 10
     characterTwoSkill = 10

     DivisionValue=round((dieTwo/dieOne),0)

     print("The number rolled on the 12 sided dice is divided by the number rolled on     the 4 sided dice")

     characterOneStrength += DivisionValue
     characterOneSkill += DivisionValue
     characterTwoStrength += DivisionValue
     characterTwoSkill += DivisionValue

     print ("The value of the divided dice is added to the character's attributes")

     print('Character one , your strength:',str(characterOneStrength) + '\n')
     print('Character one, your strength:',str(characterOneSkill) + '\n')
     print('Character two, your strength:',str(characterTwoStrength) + '\n')
     print('Character two, your strength:' ,str(characterTwoSkill) + '\n')


     fileObj = open("CharacterAttributes.txt","w") 
     fileObj.write('str(CharacterNameOne),your strength:' + str(characterOneStrength) + '\n')
     fileObj.write('str(characterNameOne), your skill:' + str(characterOneSkill) + '\n')
     fileObj.write('str(characterNameTwo),your strength:' + str(characterTwoStrength) + '\n')
     fileObj.write('str(characterNameTwo), your skill:' + str(characterTwoSkill) + '\n')
     fileObj.close()

Hi , I wrote this code as a draft for a controlled assessment at school . The task is :

When determining certain characteristics of a game character the numbers on a combination of dice are used to calculate certain attributes.

Two of these attributes are strength and skill.

At the start of the game, when the characters are created, a 4 sided dice and a 12 sided dice are thrown to determine values for each of these attributes using the following method for each character:

Each attribute is initially set to 10. The score on the 12 sided dice is divided by the score on the 4 sided dice and rounded down. This value is added to the initial value. This process is repeated for each attribute for each character. Describe this process using a suitable algorithm.

Write and test the code to determine these two attributes for a character and store the sample data for two characters, including suitable names, in a file.

I wanted to know in this piece of code how to add in the variable that has the character name the user inputs. I have tried but it wont work :

print('Character one , your strength:',str(characterOneStrength) + '\n')

Also , any suggestions on how I could make the code shorter or more efficient. Thanks

1
  • 2
    What's the exception that's raised? Also throw it up on CodeReview if you'd like that part of your question to get addressed. Commented Feb 26, 2014 at 18:44

3 Answers 3

1

String formatting should work for any case you need

message = "Character x, your strength: {} \n".format(characterXStrength)
print(message)

Note that print inherently adds a newline character already, so only include \n if you want two of them.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the Right Way (tm) to do it, IMO.
0
    print('Character one , your strength:',str(characterOneStrength) + '\n') 

is almost there: it should be

    print("character one your strength is:"+str(characterOneStrength)+"

you need to have the plus on both sides

Comments

0

Here is an extended version - trace it through, you should learn lots:

import random
import sys

if sys.hexversion < 0x3000000:
    # Python 2.x
    inp = raw_input
    rng = xrange
else:
    # Python 3.x
    inp = input
    rng = range

NAMES = [
    "Akhirom", "Amalric", "Aratus", "Athicus", "Bragoras", "Cenwulf", "Chiron",
    "Crassides", "Dayuki", "Enaro", "Farouz", "Galbro", "Ghaznavi", "Godrigo",
    "Gorulga", "Heimdul", "Imbalayo", "Jehungir", "Karanthes", "Khossus"
]
NUM_CHARS = 5
OUTPUT = "CharacterAttributes.txt"

def roll(*args):
    """
    Return the results of rolling dice
    """
    if len(args) == 1:      # roll(num_sides)
        sides, = args
        return random.randint(1, sides)
    elif len(args) == 2:    # roll(num_dice, num_sides)
        num, sides = args
        return sum(random.randint(1, sides) for _ in rng(num))
    else:
        raise TypeError("roll() takes 1 or 2 arguments")

class Character:
    def __init__(self, name=None):
        if name is None:
            name = inp("Please input character name: ").strip()
        self.name = name
        self.strength = 10 + roll(10) // roll(4)
        self.skill    = 10 + roll(10) // roll(4)

    def __str__(self):
        return "{}: strength {}, skill {}".format(self.name, self.strength, self.skill)

def main():
    # generate names  (assumes len(NAMES) >> NUM_CHARS)
    names = random.sample(NAMES, NUM_CHARS - 1)

    # make a character for each name
    chars = [Character(name) for name in names]

    # add an unnamed character (prompt for name)
    chars.append(Character())

    # write characters to file
    with open(OUTPUT, "w") as outf:
        for ch in chars:
            outf.write("{}\n".format(ch))

if __name__=="__main__":
    main()

and, for interest's sake, here is the expected-value distribution for the strength and skill attributes:

10: ******     (15.0%)
11: ********** (25.0%)
12: *********  (22.5%)
13: *****      (12.5%)
14: ***        ( 7.5%)
15: **         ( 5.0%)
16: *          ( 2.5%)
17: *          ( 2.5%)
18: *          ( 2.5%)
19: *          ( 2.5%)
20: *          ( 2.5%)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.