- 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
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.
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.

