Page 1 of 2 12 Last
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### Bit of Help please :X

Hey ppl doing controlled assesment Computing got this task where I have to do this but the problem is actually storing it how will I do it ,I know f.write(..."r+") but I am not sure it what way i need to store strength,luck,agility,skill THX (copy+paste python)

import random
import math
import time

print "Hello and Welcome to Mirko Ramphul Charater building"
print "GOOD LUCK"

def repeat():

strength=10
skill=10
agility=10
luck=10
print""

print "....calculating STRENGTH:"
A=random.randint(1,12)
B=random.randint(1,4)
print "you have thrown",A,"on the twelve sided dice"
print "you have thrown",B,"on the four sided Dice"
A=float(A)
extra_strength=A/B
print "Divided......=",extra_strength
extra_strength=math.trunc(extra_strength)
strength=strength+extra_strength
print "Giving you a Final strength of",strength
print""
time.sleep(1)

print "....calculating SKILL:"
C=random.randint(1,12)
D=random.randint(1,4)
print "you have thrown",C,"on the twelve sided dice"
print "you have thrown",D,"on the four sided Dice"
C=float(C)
extra_skill=C/D
print "Divided......=",extra_skill
extra_skill=math.trunc(extra_skill)
skill=skill+extra_skill
print "Giving you a Final skill of",skill
print ""
time.sleep(1)

print "....calculating AGILITY:"
E=random.randint(1,12)
F=random.randint(1,4)
print "you have thrown",E,"on the twelve sided dice"
print "you have thrown",F,"on the four sided Dice"
E=float(E)
extra_agility=E/F
print "Divided......=",extra_agility
extra_agility=math.trunc(extra_agility)
agility=agility+extra_agility
print "Giving you a Final agility of",agility
print ""
time.sleep(1)

print "....calculating LUCK:"
G=random.randint(1,12)
H=random.randint(1,4)
print "you have thrown",G,"on the twelve sided dice"
print "you have thrown",H,"on the four sided Dice"
G=float(G)
extra_luck=G/H
print "Divided......=",extra_luck
extra_luck=math.trunc(extra_luck)
luck=luck+extra_luck
print "Giving you a Final luck of",luck
print ""
time.sleep(1)

print "This Gives",name,"a Final Stats of:"
print strength,"STRENGTH"
print skill,"SKILL"
print agility,"Agility"
print luck,"Luck"
again()

f = open("Charater-Attributes.txt", "r+")

def again():
new_character=raw_input("Would you like to create another character:").lower()

if new_character == "yes":
print"----------------------------NEW-CHARACTER---------------------------"
repeat()

if new_character == "no":
print "Thank you for using Mirko character Building"
quit()
again()

else:
again()

repeat()
2. To use the DDCharacter class the repeat function must return

name, strength, skill, agility, luck

Code:
```class DDCharacter
def __init__(self,file_name=None):
try:
with open(file_name) as inf:
except:
file_name = self.name + '.stats'
t = (self.name, self.strength, self.skill, self.agility, self.luck) = repeat()
with open(file_name, 'w') as ouf:
ouf.write('{}\n{}\n{}\n{}\n{}\n'.format(*t))
print('character stored on file {}'.format(file_name))```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### Thanks Alot

Thanks for helping me but can you just put it with my whole code so it flow nicly and it will be easier because I don't where to put it and how and it need to flow in a loop unless you don't want to continue.

• Mekire disagrees
4. #### 2 major issues with your code

There's the problem of your question. How do I store the character's "stats"? The related questions, of which you don't seem aware, may be important too. "Why would I want to store the `stats'?", "How can I recover them?". My class constructor addresses this issue. If the character file doesn't exist, it calls your repeat function to generate initial values and interact with the player. And then it writes the data to the file. If the character file does exist the constructor loads the data.

The other egregious problem of your 4 times duplicated code should earn you a C on the exercise*. I wrote a replacement function of two arguments to handle each case independently. I didn't bother to post it because well, when you don't have something nice to say, say nothing.

* The grade is C. Cannot fail because the program (as I recall) worked. Cannot give D because there might be non-working programs with some good features. We need room for these cases. Cannot go higher than C.
Last edited by b49P23TIvg; January 7th, 2014 at 09:33 PM.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### Chears for the help:)

***DELEATED***
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### question?

Instead of repeating the steps by duplicating whats another way of writting?example plz
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

***DELEATED***
8. Had you written a function for your copy and pasted long expression then you would need to change your program in only one place upon discovering you'd put the closing parenthesis to "trunc" in the wrong place.
Code:
```def roll(n, d):
return math.trunc(float(random.randint(1,n))/random.randint(1,d))

strength += roll(12,4)
skill    += roll(12,4)
agility  += roll(12,4)
luck     += roll(12,4)```
There's no obvious reason for opening a file, nor is there a reasonable explanation for naming the function "repeat".
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### Thanks Alot still got lots of work to do

Ok I start from the beginning Hello,my name is Mirko I am doing a GCSE computing task there is task 1,2 and 3 here are the specification.I will update my work on as I get it finished and if you will be kind(you don't have to)you can help me to imporve it P.S This must be dead easy stuff for you.

A game uses dice with 4, 6 and 12 sides to determine various outcomes.
Design, code and test a program that will simulate throwing dice with these numbers of sides.
The user should be able to input which dice is being thrown, eg 4, 6 or 12.
The program should output the dice chosen and the score, for example ‘6 sided dice thrown, score 4’
The user should be able to repeat this process as many times as required.

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.

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 am really woried about task 2 and 3 loosing mark on efficient section.If help me you will change MY LIFE CAUSE Depent of this.I am not asking you but I would really apriciate if you refine section 2 and 3 so good so its a decent grade you help me a lot anyway sorry about being so anoying THANK YOU FOR EVERYTHING COMING STRAINGHT FROM MY HEART
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### Hey bit of help(lol,not again)

***DELEATED***
11. #### B-

I can't grade your program lower than C. It works, mostly. Spelling errors include:
gratest, reconised, apointed.
Many global variables, no use of argument passing through function interfaces.
Because it's written recursively I can make it crash by feeding it enough lines containing x
Code:
```\$ yes x | python mirko.py
Hello and welcome to Mirko's Gladiatoral arena fight

There was once a legendary Fighter He was summoned to Mirko's Great arena.
He was about to prove himself by to be the gratest of all.

Please enter the name of your legendary fighter:Please enter the strength of x(must 1M or below):Fighter strength must be a whole number.
Please enter the strength of x(must 1M or below):Fighter strength must be a whole number.
Please enter the strength of x(must 1M or below):Fighter strength must be a whole number.
Please enter the strength of x(must 1M or below):Fighter strength must be a whole number.
Please enter the strength of x(must 1M or below):Fighter strength must be a whole number.
...
RuntimeError: maximum recursion depth exceeded
yes: standard output: Broken pipe
yes: write error```
Still, I or others anyway can imagine other "working programs" that are much worse. I'll start with B-, thinking that you're likely to move to a higher grade based on the performance of the rest of the class.
12. As a new member, you probably missed part of the Forum Introduction and Rules, so I'll explain something important to you. For future note, what you need to do in order to get the code to remain formatted correctly is to put the code samples in [CODE] or [highlight] tags, like so:

[code]
this is an example
of indented code.
[/code]

Which will render like this:

Code:
```    this is an example
of indented code.```
You can use the '#' or highlight marker buttons at the top of the editing window to automatically insert the code into the text for you.

Here is your code again, indented properly:

Code:
```import random
import time
import math
from random import choice

possible_names=["Dragon","Spartan","Kinight","Ninja","Skilled archer","Arena champion","Barbarian"

def play_again():
print "Hello and welcome to Mirko's Gladiatoral arena fight"
time.sleep(2)
print""
print"There was once a legendary Fighter He was summoned to Mirko's Great arena."
time.sleep(2)
print"He was about to prove himself by to be the gratest of all."
time.sleep(2)
print""
global fighter_name
again_fighter_strength()

def again_fighter_strength():
global fighter_strength
global fighter_name
fighter_strength=raw_input("Please enter the strength of "+fighter_name+"(must 1M or below):")
if fighter_strength.isdigit():
fighter_strength=int(fighter_strength)
if fighter_strength < 1000001:
again_fighter_skill()
else:
print "Fighter strength must 1M or below"
again_fighter_strength()
else:
print "Fighter strength must be a whole number."
again_fighter_strength()

def again_fighter_skill():
global fighter_skill
if fighter_skill.isdigit():
fighter_skill=int(fighter_skill)
if fighter_skill < 1000001:
print""
enemy()
else:
print "Fighter Skill must 1M or below."
again_fighter_skill()
else:
print "Fighter skill must be a whole number"
again_fighter_skill()

def enemy():
global possible_names
global enemy_name
enemy_name=random.choice(possible_names)
print "The great golden gate open and you see a",enemy_name
again_enemy_strength()

def again_enemy_strength():
global enemy_strength
enemy_strength=raw_input("Please enter the strength of the "+enemy_name+"(must 1M or below):")
if enemy_strength.isdigit():
enemy_strength=int(enemy_strength)
if enemy_strength < 1000001:
again_enemy_skill()
else:
print enemy_name,"Enemy strength must 1M or below."
again_enemy_strength()
else:
print "Enemy strength must be a whole number"
again_enemy_strength()

def again_enemy_skill():
global enemy_skill
global enemy_name
enemy_skill=raw_input("Please enter the skill of the "+enemy_name+"(must 1M or below):")
if enemy_skill.isdigit():
enemy_skill=int(enemy_skill)
if enemy_skill < 1000001:
arena()
else:
print enemy_name,"Fighter skill must be a whole number"
again_enemy_skill()
else:
print "Fighter Skill must 1M or below"
again_enemy_skill()

def arena():
global fighter_name
global enemy_name
global fighter_strength
global fighter_skill
global enemy_strength
global enemy_skill
print""
print fighter_name,"stare at the",enemy_name
strength_difference=fighter_strength-enemy_strength
global skill_difference
skill_difference=fighter_skill-enemy_skill

if strength_difference > 0:
global strength_modifier
strength_modifier=math.trunc((fighter_strength-enemy_strength)/5)
print "You are stronger than the enemy by",strength_modifier
skill_mod_finder()
else:
strength_modifier=math.trunc((enemy_strength-fighter_strength)/5)
print "You are weaker than the enemy by",strength_modifier
skill_mod_finder()

def skill_mod_finder():
if skill_difference > 0:
global skill_modifier
skill_modifier=math.trunc((fighter_skill-enemy_skill)/5)
print "You are more skilful than the enemy by",skill_modifier
battle()

else:
skill_modifier=math.trunc((enemy_skill-fighter_skill)/5)
print "You are less skilful than the enemy by",skill_modifier
battle()

def battle():
time.sleep(2)
global fighter_name
global enemy_name
global fighter_strength
global fighter_skill
global enemy_strength
global enemy_skill
global strength_modifier
global skill_modifier

print "\n+++++++++++++++++++++++++++++++++++BATTLE++++++++++++++++++++++++++++++++++++++"

fighter_dice=random.randint(1,6)
enemy_dice=random.randint(1,6)

print fighter_name,"rolled a",fighter_dice
print enemy_name,"rolled a",enemy_dice

if fighter_dice == enemy_dice:
print "Dice rolled are the same,No changes are made"
battle()

elif fighter_dice > enemy_dice:
fighter_strength=fighter_strength+strength_modifier
fighter_skill=fighter_skill+skill_modifier
enemy_strength=enemy_strength-strength_modifier
enemy_skill=enemy_skill-skill_modifier

if fighter_skill <= 0:
fighter_skill=0

if enemy_skill <= 0:
enemy_skill=0

print fighter_name,"win"
print fighter_name,"now has",fighter_strength,"strength and",fighter_skill,"skill."
print enemy_name,"now has ",enemy_strength,"strength and",enemy_skill,"skill."
battle_over()

else:
fighter_strength=fighter_strength-strength_modifier
fighter_skill=fighter_skill-skill_modifier
enemy_strength=enemy_strength+strength_modifier
enemy_skill=enemy_skill+skill_modifier

if fighter_skill <= 0:
fighter_skill=0

if enemy_skill <= 0:
enemy_skill=0

print fighter_name,"lose"
print fighter_name,"now has",fighter_strength,"strength and",fighter_skill,"skill."
print enemy_name,"now has",enemy_strength,"strength and",enemy_skill,"skill."
battle_over()

def battle_over():
global fighter_name
global enemy_name

if fighter_strength <= 0:
print "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>THE><END<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
print fighter_name,"was ruthlessly defeated by a",enemy_name,"in battle and died an........"
print "..hourable death stories were told told about you but you weren't proven to be the best."
play_again_question()

elif enemy_strength <= 0:
print"\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>THE><END<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n","\n"
print "after a long exausting battle",fighter_name,"defeated",enemy_name,"in battle."
print "He lived on to be the greatest legend of all time he was reconised and respected"
print fighter_name,"he was apointed command of the gratest army of all time,He later retired"
print "to take the throne and his legend was told by everyone for years to come...."
play_again_question()

else:
print "The battle is over but the fight is not over yet"
battle()

def play_again_question():
print "\n===================================RESTART======================================"
tryagain=raw_input("Do you want to play again type in yes/no:").lower()
print""
if tryagain == "yes":
play_again()

if tryagain=="no":
print "Thank you Mirko arena fight"
quit()
play_again_question()

else:
print "Invalid option try again"
play_again_question()

play_again()```
13. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### Just out of interest

def roll():
return math.trunc(float(random.randint(1,12))/random.randint(1,4))

strength += roll()
skill += roll()
agility += roll()
luck += roll()

[/code]Does that work too [/QUOTE]
14. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

#### A favour?

You got any email I just send you my code I don't want people to copy mine .I am nearly done just need some few guidance to top the grade.I am very gratefull of your help and a apriciate it a lot even if it just look like some text.it won't let me private message you.I am forever grateful for that. I will always remember everything you told me. It helped me a lot.
15. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
16
Rep Power
0

Code:
```import math
import random
print "Hello and Welcome to Mirko Ramphul Charater building"
print "GOOD LUCK"

def repeat():

def roll():
return math.trunc(float(random.randint(1,12))/random.randint(1,4))

strength = 10
skill    = 10
agility  = 10
luck     = 10

strength = roll()+strength
skill    = roll()+skill
agility  = roll()+agility
luck     = roll()+luck

print "This Gives",name,"a Final Stats of:"
print strength,"STRENGTH"
print skill,"SKILL"
print agility,"Agility"
print luck,"Luck"

myfile=open("Character_stats.txt", "w")

myfile.write(name + " has a stat of:" + "\n")
myfile.write("strength="+str(strength) + "\n")
myfile.write("skill="+str(skill) + "\n")
myfile.write("agility="+str(agility) + "\n")
myfile.write("luck="+str(luck))
again()

def again():
new_character=raw_input("Would you like to create another character:").lower()
if new_character=="yes":
print"----------------------------NEW-CHARACTER------------------------"
repeat()

if new_character=="no":
print "Thank you for using Mirko character Building"
quit()
again()

else: