[3.2] Assistance on making code more efficient

Joined
Apr 5, 2012
Messages
2,003
Reaction score
827
Hi guys,

for my grade in computing we are required to complete an exam and two controlled assessments, one being based entirely on programming, the language we have been given is Python.

The certain controlled assessment is based around an idea from 'Dungeons and Dragons' the task I am on and have done is about giving the user the set skills and strength points what have been generated with a dice roll and some calculations.

Basically, I have completed the task and I want to make it more efficient in less lines of code and probably make it look cleaner, that's why I am posting here, I am currently unsure on how I can reduce the amount of lines of code I have used already.

Brief desc of task:

You have to ask the user for the names of two characters, the program you have made will then generate the skills and strengths for this characters. The dice will need to be done for both attributes (4 and 12 sided die). You will then need to divide the number from both dices by each other then add 10 to that answer. You would then need to round that answer down if it is a decimal. You then need to export the data to a file for the user to read after closing the program



This is the code I have written for that task

Code:
[COLOR="#FF8C00"]import[/COLOR] random

Char1=[COLOR="#800080"]input[/COLOR]([COLOR="#00FF00"]"What is the first characters name?:"[/COLOR])
Char2=[COLOR="#800080"]input[/COLOR]([COLOR="#00FF00"]"What is the second characters name?:"[/COLOR])

Char1Skill = random.randint(1,4)
Char2Skill = random.randint(1,4)

Char1Skill2= random.randint (1,12)
Char2Skill2= random.randint (1,12)

attribute = 10

Char1Strength1=random.randint(1,4)
Char2Strength1=random.randint(1,4)

Char1Strength2=random.randint(1,4)
Char2Strength2=random.randint(1,4)

Char1SkillEnd = Char1Skill2 / Char1Skill +attribute
Char2SkillEnd = Char2Skill2 / Char2Skill +attribute

Char1StrEnd = Char1Strength2 / Char1Strength1 +attribute
Char2StrEnd = Char2Strength2 / Char2Strength1 + attribute

[COLOR="#800080"]print[/COLOR] ([COLOR="#00FF00"]"Character One: {0}\n Skill: {1}\n Strength: {2}"[/COLOR].format(Char1,Char1SkillEnd,Char1StrEnd))
[COLOR="#800080"]print[/COLOR] ([COLOR="#00FF00"]"Character Two: {0}\n Skill: {1}\n Strength: {2}"[/COLOR].format(Char2,Char2SkillEnd,Char2StrEnd))



[COLOR="#FF8C00"]with[/COLOR] [COLOR="#800080"]open[/COLOR][COLOR="#00FF00"]("test.txt"[/COLOR], [COLOR="#00FF00"]"wt"[/COLOR]) [COLOR="#FF8C00"]as[/COLOR] out_file:
    out_file.write([COLOR="#00FF00"]"Character One: {0}\n Skill:{1}\n Strength: {2}"[/COLOR].format(Char1,Char1SkillEnd,Char1StrEnd))
    out_file.write([COLOR="#00FF00"]"Character Two: {0}\n Skill:{1}\n Strength: {2}"[/COLOR].format(Char2,Char2SkillEnd,Char2StrEnd))

So that's where I need the help, if you can help me by basically saying where I could reduce the amount of code and how I could do it.

Thanks.
 
I was going to explain what you should do but I figured it'd be easier to just show you. Please don't just copy & paste this however.
So what I consider important here are the ways to avoid repeating yourself:
* Character as a class has its initialization, which can then be called a number of times
* There's the common way to present the character as text, so make that a member function of the class
* Minor: random.randint(1,12) doesn't tell you a whole lot about the meaning, so abstract that away to rollDie(sides) function

Code:
import random
import math

def rollDie(sides):
  return random.randint(1, sides)

class Char:
  def __init__(self, name):
    self.name   = name

    self.skill  = rollDie(4)
    self.skill2 = rollDie(12)

    self.strength  = rollDie(4)
    self.strength2 = rollDie(4)

    base = 10
    self.skillEnd = math.trunc(self.skill2 / self.skill + base)
    self.strEnd   =  math.trunc(self.strength2 / self.strength + base)

  def present(self):
    return "{0}\n Skill: {1}\n Strength: {2}".format(self.name, self.skillEnd, self.strEnd)

char1 = Char(input("What is the first characters name?:"))
char2 = Char(input("What is the second characters name?:"))

print("Character One: " + char1.present())
print("Character Two: " + char2.present())

with open("test.txt", "wt") as out_file:
  out_file.write("Character One: "+ char1.present())
  out_file.write("Character Two: "+ char2.present())
 
Back