You are viewing our Forum Archives. To view or take place in current topics click here.
Python. Anyways on making shorter
Posted:

Python. Anyways on making shorterPosted:

Redacted
  • Summer 2018
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
Is there anyway on making this shorter or more clean?

Just something ive had to do for school as we've started learning python.

Thanks in advanced.

Name= input("what is you're hero's name? ")

print("Select Class.")
print("1. Warrior")
print("2. Mage")
print("3. Peasant")
print("4. Archer")

Class= input("Enter Class name ")



if Class == 'Warrior':
print("You have chosen Warrior")

elif Class == 'Mage':
print("You have chosen Mage")

elif Class == 'Peasant':
print("You have chosen Peasant")

elif Class == 'Archer':
print("You have chosen Archer")
else:
print("Please pick a class.")

print("Select Race.")
print("1. Altmer")
print("2. Argonian")
print("3. Bosmer")
print("4. Breton")
print("5. Dunmer")
print("6. Imperial")
print("7. Khajiit")
print("8. Nord")
print("9. Orsimer")
print("10. Redguard")


Race = input("What Race are you? ")


if Race == 'Altmer':

print("You have chosen Altmer")

elif Race == 'Argonian':
print("You have chosen Argonion")

elif Race == 'Bosmer':
print("You have chosen Bosmer")

elif Race == 'Breton':
print("You have chosen Breton")

elif Race == 'Dunmer':
print("You have chosen Dunmer")

elif Race == 'Imperial':
print("You have chosen Imperial")

elif Race == 'Khajiit':
print("You have chosen Khajiit")

elif Race == 'Nord':
print("You have chosen Nord")

elif Race == 'Orsimer':
print("You have chosen Orsimer")

elif Race == 'Redgaurd':
print("You have chosen Redgaurd")

else:
print("Please select a Race")

print("Select a Gender")
print("Male")
print("Female")

Gender = input("Enter gender ")

if Gender == 'Male':
print("You have chosen to be a male")

elif Gender == 'Female':
print("You have chosen to be a Female")

else:
print("Please select a gender")



print("Welcome",Name,",You are a trained",Gender,Class,"from the",Race,"race")

Choice=input("Do you wish to continue? Yes or no ")
if Choice == "yes":
print("Welcome! Set fourth on your adventure")

elif Choice == "no":
print("you coward!")


def Moneytoman():

Moneycount= 1000

print("You have",Moneycount,"coins")

Give = int(input("On your journey down the path you see a homless man. How much Money do you wish to give him? "))

if Give < 1:
print("Thats not nice")

elif Give > 1000:
print("you dont have that much")
Moneytoman()


else:
print("Thanks")


Moneycount= Moneycount - Give
print("You now have",Moneycount, "coins left")

Moneytoman()

Bag = ["A Dagger","A Note","A piece of Rope"]

pickingupbagdecision = input("Further down the path you spot a bag lying on the floor. Press Y to pick it up or N to leave it there ")

if pickingupbagdecision == "Y":
Baglook = input("You picked up the bag, do you want to look inside? Press Y to look inside or press N to wait until later ")
if Baglook == "Y":
print("Inside is a")
print(Bag)

else:
print("we'll look later")

elif pickingupbagdecision == "N":
print("You left the bag on the floor")











#2. Posted:
Deftones
  • Winner!
Status: Offline
Joined: May 16, 201310Year Member
Posts: 8,519
Reputation Power: 1554
Status: Offline
Joined: May 16, 201310Year Member
Posts: 8,519
Reputation Power: 1554
Actually looks pretty cool, but with my experience with Python, I do not know.
#3. Posted:
Redacted
  • Christmas!
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
ZTG wrote Actually looks pretty cool, but with my experience with Python, I do not know.


Oh alright mate, Thanks anyways
#4. Posted:
PeakModz
  • Challenger
Status: Offline
Joined: Nov 29, 201112Year Member
Posts: 145
Reputation Power: 8
Status: Offline
Joined: Nov 29, 201112Year Member
Posts: 145
Reputation Power: 8
Jack_Sparrow wrote Added a lot more


Level = 1
BaseHealth = 200
Health = BaseHealth*0.5 * Level
Armour = 0
BaseDamage = 10
LevelDamage = 1.13** Level
LevelDamagemod = BaseDamage // LevelDamage
Damage = BaseDamage * LevelDamagemod
LevelUpMod = 1.2

EnemyLevel = Level // 0.5
BaseEnemyHealth = 100
EnemyHealth = BaseEnemyHealth*0.7 * EnemyLevel
EnemyBaseDamage = 10
EnemyLevelDamage = 1.13** EnemyLevel
EnemyLevelDamageMod = EnemyBaseDamage // EnemyLevelDamage
EnemyDamage = EnemyBaseDamage * EnemyLevelDamageMod
XP = 0
XPRequired = Level * 100 +50
XPEarned= EnemyLevel * 100 *0.75

Attack = input("Press A to attack! ")
if Attack == "A":
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EnemyHealth - Damage,"Health left")
EH = EnemyHealth - Damage
if EH <= 0:
print("You have defeated the enemy!")
elif EH > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", Health - EnemyDamage,"Health left")
YH = Health - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH - Damage,"Health left")
EH1 = EH - Damage
if EH1 <= 0:
print("You have defeated the enemy!")

elif EH1 > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", YH - EnemyDamage,"Health left")
YH1 = YH - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH1 - Damage,"Health left")
EH2 = EH1 - Damage
if EH2 <= 0:
print("You have defeated the enemy!")


print("You have gained", XPEarned,"XP")
XP = XP + XPEarned
XPRequired1 = XPRequired - XP
if XPRequired1 <= XPRequired:
Level = Level + 1
print("You have leveled up to level", Level,)
Health = Health * LevelUpMod
Damage = Damage * LevelUpMod
print("health increased to", Health)
print("Damage increased to", Damage)

else:
print("Lets move on")

print("Lets move on")


Would this work in a long run game. If i keep updating variables after each battle etc.


Instead of using pure sequence and selection try and add some iteration, while loops etc. It will make your code so much easier if they can be implemented anywhere. Also add a few functions for main elements of the program.

E.g

def functionName(x):

return 0;
#5. Posted:
Redacted
  • Comment King
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
PeakModz wrote
Jack_Sparrow wrote Added a lot more


Level = 1
BaseHealth = 200
Health = BaseHealth*0.5 * Level
Armour = 0
BaseDamage = 10
LevelDamage = 1.13** Level
LevelDamagemod = BaseDamage // LevelDamage
Damage = BaseDamage * LevelDamagemod
LevelUpMod = 1.2

EnemyLevel = Level // 0.5
BaseEnemyHealth = 100
EnemyHealth = BaseEnemyHealth*0.7 * EnemyLevel
EnemyBaseDamage = 10
EnemyLevelDamage = 1.13** EnemyLevel
EnemyLevelDamageMod = EnemyBaseDamage // EnemyLevelDamage
EnemyDamage = EnemyBaseDamage * EnemyLevelDamageMod
XP = 0
XPRequired = Level * 100 +50
XPEarned= EnemyLevel * 100 *0.75

Attack = input("Press A to attack! ")
if Attack == "A":
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EnemyHealth - Damage,"Health left")
EH = EnemyHealth - Damage
if EH <= 0:
print("You have defeated the enemy!")
elif EH > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", Health - EnemyDamage,"Health left")
YH = Health - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH - Damage,"Health left")
EH1 = EH - Damage
if EH1 <= 0:
print("You have defeated the enemy!")

elif EH1 > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", YH - EnemyDamage,"Health left")
YH1 = YH - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH1 - Damage,"Health left")
EH2 = EH1 - Damage
if EH2 <= 0:
print("You have defeated the enemy!")


print("You have gained", XPEarned,"XP")
XP = XP + XPEarned
XPRequired1 = XPRequired - XP
if XPRequired1 <= XPRequired:
Level = Level + 1
print("You have leveled up to level", Level,)
Health = Health * LevelUpMod
Damage = Damage * LevelUpMod
print("health increased to", Health)
print("Damage increased to", Damage)

else:
print("Lets move on")

print("Lets move on")


Would this work in a long run game. If i keep updating variables after each battle etc.


Instead of using pure sequence and selection try and add some iteration, while loops etc. It will make your code so much easier if they can be implemented anywhere. Also add a few functions for main elements of the program.

E.g

def functionName(x):

return 0;


I havent quite got around to understanding how to use loops yet and that. I just made a new post with all my code on.

Thanks though, i will defo look into def functions and while/for loops to make the code better
#6. Posted:
PeakModz
  • Challenger
Status: Offline
Joined: Nov 29, 201112Year Member
Posts: 145
Reputation Power: 8
Status: Offline
Joined: Nov 29, 201112Year Member
Posts: 145
Reputation Power: 8
Jack_Sparrow wrote
PeakModz wrote
Jack_Sparrow wrote Added a lot more


Level = 1
BaseHealth = 200
Health = BaseHealth*0.5 * Level
Armour = 0
BaseDamage = 10
LevelDamage = 1.13** Level
LevelDamagemod = BaseDamage // LevelDamage
Damage = BaseDamage * LevelDamagemod
LevelUpMod = 1.2

EnemyLevel = Level // 0.5
BaseEnemyHealth = 100
EnemyHealth = BaseEnemyHealth*0.7 * EnemyLevel
EnemyBaseDamage = 10
EnemyLevelDamage = 1.13** EnemyLevel
EnemyLevelDamageMod = EnemyBaseDamage // EnemyLevelDamage
EnemyDamage = EnemyBaseDamage * EnemyLevelDamageMod
XP = 0
XPRequired = Level * 100 +50
XPEarned= EnemyLevel * 100 *0.75

Attack = input("Press A to attack! ")
if Attack == "A":
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EnemyHealth - Damage,"Health left")
EH = EnemyHealth - Damage
if EH <= 0:
print("You have defeated the enemy!")
elif EH > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", Health - EnemyDamage,"Health left")
YH = Health - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH - Damage,"Health left")
EH1 = EH - Damage
if EH1 <= 0:
print("You have defeated the enemy!")

elif EH1 > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", YH - EnemyDamage,"Health left")
YH1 = YH - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH1 - Damage,"Health left")
EH2 = EH1 - Damage
if EH2 <= 0:
print("You have defeated the enemy!")


print("You have gained", XPEarned,"XP")
XP = XP + XPEarned
XPRequired1 = XPRequired - XP
if XPRequired1 <= XPRequired:
Level = Level + 1
print("You have leveled up to level", Level,)
Health = Health * LevelUpMod
Damage = Damage * LevelUpMod
print("health increased to", Health)
print("Damage increased to", Damage)

else:
print("Lets move on")

print("Lets move on")


Would this work in a long run game. If i keep updating variables after each battle etc.


Instead of using pure sequence and selection try and add some iteration, while loops etc. It will make your code so much easier if they can be implemented anywhere. Also add a few functions for main elements of the program.

E.g

def functionName(x):

return 0;


I havent quite got around to understanding how to use loops yet and that. I just made a new post with all my code on.

Thanks though, i will defo look into def functions and while/for loops to make the code better


If you need any help, Python is a language I know, so just send me a PM
#7. Posted:
Redacted
  • Christmas!
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
PeakModz wrote
Jack_Sparrow wrote
PeakModz wrote
Jack_Sparrow wrote Added a lot more


Level = 1
BaseHealth = 200
Health = BaseHealth*0.5 * Level
Armour = 0
BaseDamage = 10
LevelDamage = 1.13** Level
LevelDamagemod = BaseDamage // LevelDamage
Damage = BaseDamage * LevelDamagemod
LevelUpMod = 1.2

EnemyLevel = Level // 0.5
BaseEnemyHealth = 100
EnemyHealth = BaseEnemyHealth*0.7 * EnemyLevel
EnemyBaseDamage = 10
EnemyLevelDamage = 1.13** EnemyLevel
EnemyLevelDamageMod = EnemyBaseDamage // EnemyLevelDamage
EnemyDamage = EnemyBaseDamage * EnemyLevelDamageMod
XP = 0
XPRequired = Level * 100 +50
XPEarned= EnemyLevel * 100 *0.75

Attack = input("Press A to attack! ")
if Attack == "A":
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EnemyHealth - Damage,"Health left")
EH = EnemyHealth - Damage
if EH <= 0:
print("You have defeated the enemy!")
elif EH > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", Health - EnemyDamage,"Health left")
YH = Health - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH - Damage,"Health left")
EH1 = EH - Damage
if EH1 <= 0:
print("You have defeated the enemy!")

elif EH1 > 0:
print("The Enemy strikes you and deals", EnemyDamage," Damage! You have", YH - EnemyDamage,"Health left")
YH1 = YH - EnemyDamage
input("Press A to attack! ")
print("You hit the enemy and deal", Damage, "Damage! leaving the enemy with",EH1 - Damage,"Health left")
EH2 = EH1 - Damage
if EH2 <= 0:
print("You have defeated the enemy!")


print("You have gained", XPEarned,"XP")
XP = XP + XPEarned
XPRequired1 = XPRequired - XP
if XPRequired1 <= XPRequired:
Level = Level + 1
print("You have leveled up to level", Level,)
Health = Health * LevelUpMod
Damage = Damage * LevelUpMod
print("health increased to", Health)
print("Damage increased to", Damage)

else:
print("Lets move on")

print("Lets move on")


Would this work in a long run game. If i keep updating variables after each battle etc.


Instead of using pure sequence and selection try and add some iteration, while loops etc. It will make your code so much easier if they can be implemented anywhere. Also add a few functions for main elements of the program.

E.g

def functionName(x):

return 0;


I havent quite got around to understanding how to use loops yet and that. I just made a new post with all my code on.

Thanks though, i will defo look into def functions and while/for loops to make the code better


If you need any help, Python is a language I know, so just send me a PM


Thanks will do. If i come across any problems or have any questions ill PM you.
#8. Posted:
-Deano
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
The only simple way I see to make this shorter is by using the "\n" character in your print statements instead of having the "print" in every line.
Besides that, there isn't too much you can do whilst being on a low knowledge level.
Shame Python doesn't have built in Select Case statements. That would've made more sense in a situation like this.
#9. Posted:
Redacted
  • Gold Member
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
Status: Offline
Joined: Mar 02, 201410Year Member
Posts: 4,724
Reputation Power: 5977
-Deano wrote The only simple way I see to make this shorter is by using the "\n" character in your print statements instead of having the "print" in every line.
Besides that, there isn't too much you can do whilst being on a low knowledge level.
Shame Python doesn't have built in Select Case statements. That would've made more sense in a situation like this.


Sorry, could you explain the \n thing. How would that work when writing it in code
#10. Posted:
DankMemes1337
  • Resident Elite
Status: Offline
Joined: Sep 15, 201211Year Member
Posts: 252
Reputation Power: 10
Status: Offline
Joined: Sep 15, 201211Year Member
Posts: 252
Reputation Power: 10
Jack_Sparrow wrote
-Deano wrote The only simple way I see to make this shorter is by using the "\n" character in your print statements instead of having the "print" in every line.
Besides that, there isn't too much you can do whilst being on a low knowledge level.
Shame Python doesn't have built in Select Case statements. That would've made more sense in a situation like this.


Sorry, could you explain the \n thing. How would that work when writing it in code


Essentially \n just signals a new line so in your code I would probably use it like this :

print("Select class:\n1. Mage\n2. etc


Also the use of functions and loops for/while would make your code look nicer and possibly more efficient.Any questions feel free to PM me and I'll try my best to help
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.