所以这是错误:
Traceback (most recent call last):
File "E:\python\cloud.py", line 593, in <module>
inventory()
File "E:\python\cloud.py", line 297, in inventory
print("Weapon Attack Damage: ", c.weaponAttack[i])
IndexError: list index out of range
这些是代码中唯一具有&#34; weaponAttack&#34;功能在其中。老实说,我不明白为什么它会给我这个错误。
class Cloud:
def __init__(self):
self.weaponAttack = list()
self.sp = 0
self.armor = list()
self.armorReduction = list()
self.weapon = list()
self.money = 0
self.lvl = 0
self.exp = 0
self.mexp = 100
self.attackPower = 0
self.hp = 100
self.mhp = 100
self.name = "Cloud"
c = Cloud()
armors = ["No Armor","Belice Armor","Yoron's Armor","Andrew's Custom Armor","Zeus' Armor"]
armorReduce = [.0, .025, .05, .10, .15]
c.armor.append(armors[0])
c.armorReduction.append(armorReduce[0])
w = random.randint(0, 10)
weapons = ["The sword of wizdom","The sword of kindness", "the sword of power", "the sword of elctricity", "the sword of fire", "the sword of wind", "the sword of ice", "the sword of self appreciation", "the sword of love", "the earth sword", "the sword of the universe"]
weaponAttack = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
c.weapon.append(weapons[w])
c.weapon.append(weaponAttack[w])
print("You have recieved the ", weapons[w])
print("")
print("It does ", weaponAttack[w]," attack power!")
print("")
for i in range(0, len(c.weapon)):
print(i)
print("Weapon: ", c.weapon[i])
print("Weapon Attack Damage: ", c.weaponAttack[i])
print("")
虽然这是代码的其余部分,但在您阅读之前,我会警告您,这很长。无论哪种方式,我都非常确定那些代码行存在问题。
import random
import time
import sys
def asky():
ask = input("Would you like to check you player stats and inventory or go to the next battle? Say inventory for inventory or say next for the next battle: ")
if "inventory" in ask:
inventory()
elif "next" in ask:
user()
def Type(t):
t = list(t)
for a in t:
sys.stdout.write(a)
time.sleep(.02)
class Cloud:
def __init__(self):
self.weaponAttack = list()
self.sp = 0
self.armor = list()
self.armorReduction = list()
self.weapon = list()
self.money = 0
self.lvl = 0
self.exp = 0
self.mexp = 100
self.attackPower = 0
self.hp = 100
self.mhp = 100
self.name = "Cloud"
c = Cloud()
armors = ["No Armor","Belice Armor","Yoron's Armor","Andrew's Custom Armor","Zeus' Armor"]
armorReduce = [.0, .025, .05, .10, .15]
c.armor.append(armors[0])
c.armorReduction.append(armorReduce[0])
w = random.randint(0, 10)
weapons = ["The sword of wizdom","The sword of kindness", "the sword of power", "the sword of elctricity", "the sword of fire", "the sword of wind", "the sword of ice", "the sword of self appreciation", "the sword of love", "the earth sword", "the sword of the universe"]
weaponAttack = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
c.weapon.append(weapons[w])
c.weapon.append(weaponAttack[w])
print("You have recieved the ", weapons[w])
print("")
print("It does ", weaponAttack[w]," attack power!")
print("")
class Soldier:
def __init__(self):
dmg = random.randint(5,20)
self.lvl = 0
self.attackPower = dmg
self.hp = 100
self.mhp = 100
self.name = "Soldier"
s = Soldier()
def enemy():
ad = random.randint(0,2)
if ad >= 1: #Attack
Type("Soldier attacks!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
hm = random.randint(0, 2)
if hm == 0:
Type("Miss!")
print("")
elif hm > 0:
crit = random.randint(0,10)
if crit == 0:
print("CRITICAL HIT!")
crithit = int((s.attackPower) * (.5))
c.hp = c.hp - (s.attackPower + crithit)
elif crit >= 1:
c.hp = c.hp - s.attackPower
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
asky()
c.exp = c.exp + 100
else:
user()
elif ad == 0:#Defend
Type("Soldier Defends!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if s.hp == s.mhp:
print("")
elif s.hp > (s.mhp - 15) and s.hp < s.mhp:
add = s.mhp - s.hp
s.hp = add + s.hp
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
elif s.hp < (s.mhp - 15):
s.hp = s.hp + 15
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
asky()
c.exp = c.exp + 100
else:
user()
def user():
User = input("attack or defend? ")
if "attack" in User:#attack
Type("Cloud attacks!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
hm = random.randint(0,4)
if hm == 0:
Type("Miss!")
print("")
elif hm > 0:
crit = random.randint(0,7)
if crit == 0:
print("CRITICAL HIT!")
crithit = int((c.weaponAttack[0]) * (.5))
s.hp = s.hp - (c.weaponAttack[0] + crithit)
elif crit >= 1:
s.hp = s.hp - c.weaponAttack[0]
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
c.exp = c.exp + 100
asky()
else:
enemy()
elif "defend" in User:#defend
Type("Cloud Heals!")
print("")
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp == c.mhp:
Type("You are at the maximum amount of health. Cannot add more health.")
print("")
elif c.hp > (c.mhp - 15) and c.hp < c.mhp:
add = c.mhp - c.hp
c.hp = add + c.hp
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
elif c.hp <= (c.mhp - 15):
c.hp = c.hp + 15
Type("Cloud Health: ")
print(c.hp)
Type("Enemy Health: ")
print(s.hp)
if c.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("GAME OVER")
print("")
Type("You Lost!")
print("")
elif s.hp <= 0:
adds = s.mhp - s.hp
s.hp = s.hp + adds
Type("Congratulations!")
print("")
Type("You Won!")
print("")
Type("You recieved 100 crystals to spend at the shop!")
print("")
c.money = c.money + 100
c.exp = c.exp + 100
asky()
else:
enemy()
else:
Type("The option you have entered is not in the game database. Please try again")
print("")
user()
def inventory():
if c.exp == c.mexp:
print("LEVEL UP!")
c.exp = 0
adde = int((c.mexp) * (.5))
c.mexp = c.mexp + adde
c.sp = c.sp + 1
c.lvl = c.lvl + 1
if c.lvl > s.lvl:
s.lvl = s.lvl + 1
print("Level: ", c.lvl)
nextlvl = c.lvl + 1
print("Experience: ", c.exp, "/", c.mexp, "level", nextlvl)
print("Amount of Skill Points:", c.sp)
for i in range(0, len(c.weapon)):
print(i)
print("Weapon: ", c.weapon[i])
print("Weapon Attack Damage: ", c.weaponAttack[i])
print("")
for j in range(0, len(c.armor)):
print("Armor: ", c.armor[j])
print("Armor Damage Reduction: ", c.armorReduction[j])
print("")
print("Amount of Crystals: ", c.money)
print("")
print("")
print("Stats:")
print("Maximum Health: ", c.mhp)
print("Current Health: ", c.hp)
print("Your Name: ", c.name)
print("")
sn = input("To heal yourself, you need to go to the shop. Say, *shop* to go to the shop, say *name* to change your name, say, *next* to fight another battle, say, *level* to use your skill point(s), or say, *help* for help: ")
if "name" in sn:
c.name = input("Enter Your name here: ")
print("Success! Your name has been changed to ", c.name)
inventory()
elif "next" in sn:
Type("3")
print("")
Type("2")
print("")
Type("1")
print("")
Type("FIGHT!")
print("")
user()
elif "help" in sn:
Type("The goal of this game is to fight all the enemies, kill the miniboss, and finally, kill the boss! each time you kill an enemy you gain *crystals*, currency which you can use to buy weapons, armor, and health. You can spend these *crystals* at the shop. To go to the shop, just say *shop* when you are in your inventory. Although, each time you defeat an enemy, they get harder to defeat. Once you level up, you gain one skill point. This skill point is then used while in your inventory by saying the word *level*. You can use your skill point(s) to upgrade your stats, such as, your maximum health, and your attack power.")
print("")
inventory()
elif "shop" in sn:
shop()
elif "level" in sn:
skills()
else:
print("Level: ", c.lvl)
nextlvl = c.lvl + 1
print("Experience: ", c.exp, "/", c.mexp, "level", nextlvl)
print("Amount of Skill Points:", c.sp)
for i in range(0, len(c.weapon)):
print("Weapon:", c.weapon[i])
print("Weapon Attack Damage: ", c.weaponAttack[i])
print("")
for i in range(0, len(c.armor)):
print("Armor: ", c.armor[i])
print("Armor Damage Reduction: ", c.armorReduction[i])
print("")
print("Amount of Crystals: ", c.money)
print("")
print("")
print("Stats:")
print("Maximum Health: ", c.mhp)
print("Current Health: ", c.hp)
print("Attack Power: ", c.attackPower)
print("Your Name: ", c.name)
print("")
sn = input("To heal yourself, you need to go to the shop. Say, *shop* to go to the shop, say *name* to change your name, say, *next* to fight another battle, say, *level* to use your skill point(s), or say, *help* for help: ")
if "name" in sn:
c.name = input("Enter Your name here: ")
print("Success! Your name has been changed to ", c.name)
inventory()
elif "next" in sn:
Type("3")
print("")
Type("2")
print("")
Type("1")
print("")
Type("FIGHT!")
print("")
user()
elif "help" in sn:
Type("The goal of this game is to fight all the enemies, kill the miniboss, and finally, kill the boss! each time you kill an enemy you gain *crystals*, currency which you can use to buy weapons, armor, and health. You can spend these *crystals* at the shop. To go to the shop, just say *shop* when you are in your inventory. Although, each time you defeat an enemy, they get harder to defeat. Once you level up, you gain one skill point. This skill point is then used while in your inventory by saying the word *level*. You can use your skill point(s) to upgrade your stats, such as, your maximum health, and your attack power.")
print("")
inventory()
elif "shop" in sn:
shop()
elif "level" in sn:
skills()
def skills():
print("You have ", c.sp, "skill points to use.")
print("")
print("Upgrade attack power *press the number 1*")
print("")
print("Upgrade maximum health *press the number 2*")
print("")
skill = input("Enter the number of the skill you wish to upgrade, or say, cancel, to go back to your inventory screen.")
if "1" in skill:
sure = input("Are you sure you want to upgrade your character attack power in return for 1 skill point? *yes or no*")
if "yes" in sure:
c.sp = c.sp - 1
addsap = c.attackPower * .01
c.attackPower = c.attackPower + addsap
if "no" in sure:
skills()
elif "2" in skill:
sure = input("Are you sure you want to upgrade your maximum health in return for 1 skill point? *yes or no*")
if "yes" in sure:
c.sp = c.sp - 1
c.mhp = c.mhp + 30
if "no" in sure:
skills()
elif "cancel" in skill:
inventory()
else:
Type("The word or number you have entered is invalid. Please try again.")
print("")
skills()
def shop():
print("Welcome to Andrew's Blacksmith! Here you will find all the weapons, armor, and health you need, to defeat the horrid beast who goes by the name of Murlor! ")
print("")
print("Who's Murlor? *To ask this question, type in the number 1*")
print("")
print("Can you heal me? *To ask this question, type in the number 2*")
print("")
print("What weapons do you have? *To ask this question, type in the number 3*")
print("")
print("Got any armor? *To ask this question, type in the number 4*")
print("")
ask1 = input("Enter desired number here or say, cancel, to go back to your inventory screen. ")
if "1" in ask1:
def murlor():
Type("Murlor is a devil-like creature that lives deep among the caves of Bricegate. He has been terrorising the people of this village for centuries.")
print("")
print("What is Bricegate? *To choose this option, type in the number 1*")
print("")
print("Got any more information about this village? *To choose this option, type in the number 2*")
print("")
print("Thank you! *To choose this option, type in the number 3*")
ask3 = input("Enter desired number here, or say, cancel, to go back to the main shop screen. ")
if "1" in ask3:
Type("That's the name of this town.")
murlor()
elif "2" in ask3:
def askquest1():
quest1 = input("Well I DO know that there's this secret underground dungeon. It's VERY dangerous but it comes with a hug reward. If you ever concider it, could you get my lucky axe? I dropped it down a hole leading to the dungeon and i was too afraid to get it back. *If you accept the quest, say yes, if you want to go back, say, no.*")
if "yes" in quest1:
quest1()
elif "no" in quest1:
murlor()
else:
Type("The option you have selected is not valid. Please try again")
print("")
askquest1()
elif "3" in ask3:
shop()
else:
Type("The number or word you have entered is invalid. please try again.")
print("")
elif "2" in ask1:
def heal():
Type("Sure! That'll be 30 crystals.")
ask2 = input(" *say, okay, to confirm the purchase or say, no, to cancel the pruchase*")
if "okay" in ask2:
if c.money < 30:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
shop()
elif c.money >= 30:
c.money = c.money - 30
Type("30 crystals has been removed from your inventory.")
print("")
addn = c.mhp - c.hp
c.hp = c.hp + addn
Type("You have been healed!")
print("")
shop()
elif "no" in ask2:
shop()
else:
Type("The option you have chosen is invalid. Please try again")
print("")
heal()
elif "3" in ask1:
def swords():
print("Swords: ")
print("The Belice Sword: *Type 1 for this sword*")
print("Damage: 18")
print("Cost: 70 crystals")
print("")
print("The Sword of A Thousand Truths: *Type 2 for this sword*")
print("Damage: 28")
print("Cost: 100 crystals")
print("")
print("Spyro's Sword: *Type 3 for this sword*")
print("Damage: 32")
print("Cost: 125 crystals")
print("")
print("The Sword Of The Athens: *Type 4 for this sword*")
print("Damage: 36")
print("Cost: 150 crystals")
print("")
print("Coming Soon...")
sword = input("Enter the sword ID number or say cancel to go back to the main shop screen. You now have ", c.money, "crystals.")
if "1" in sword:
if c.money < 70:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 70:
c.money = c.money - 70
Type("70 crystals has been removed from your inventory.")
print("")
weapon.append("The Belice Sword")
Type("The Belice Sword has been added to your inventory!")
print("")
swords()
elif "2" in sword:
if c.money < 250:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 250:
c.money = c.money - 250
Type("250 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("The Sword Of A Thousand Truths")
Type("The Sword Of A Thousand Truths has been added to your inventory!")
print("")
swords()
elif "3" in sword:
if c.money < 525:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 525:
c.money = c.money - 525
Type("525 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("The Spyro's Sword")
Type("The Spyro's Sword has been added to your inventory!")
print("")
swords()
elif "4" in sword:
if c.money < 1050:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
swords()
elif c.money >= 1050:
c.money = c.money - 1050
Type("1050 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("The Sword Of The Athens")
Type("The Sword Of The Athens has been added to your inventory!")
print("")
swords()
elif "cancel" in sword:
shop()
else:
Type("The number or word you have entered is invalid. Please try again.")
print("")
swords()
elif "4" in ask1:
def armory():
print("Armor:")
print("Belice Armor, ID: 1")
print("Damage Reduction: 2.5%")
print("Cost: 100 crystals")
print("")
print("Yoron's armor, ID: 2")
print("Damage Reduction: 5%")
print("Cost: 250 crystals")
print("")
print("Andrew's Custom Armor, ID: 3")
print("Damage Reduction: 10%")
print("Cost: 500 crystals")
print("")
print("Zeus' Armor, ID: 4")
print("Damage Reduction: 15%")
print("Cost: 1000 crystals")
print("")
print("Coming Soon...")
print("")
armor = input("Enter armor ID number, or type, cancel, to go back to the main shop menu.")
if "1" in armor:
if c.money < 100:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 100:
c.money = c.money - 100
Type("100 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Belice Armor")
Type("Belice Armor has been added to your inventory!")
print("")
armory()
elif "2" in armor:
if c.money < 250:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 250:
c.money = c.money - 250
Type("250 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Yoron's Armor")
Type("Yoron's Armor has been added to your inventory!")
print("")
armory()
elif "3" in armor:
if c.money < 500:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 500:
c.money = c.money - 500
Type("500 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Andrew's Custom Armor")
Type("Andrew's Custom Armor has been added to your inventory!")
print("")
armory()
elif "4" in armor:
if c.money < 1000:
Type("I'm sorry sir, but you don't have enough crystals to buy this.")
print("")
armory()
elif c.money >= 1000:
c.money = c.money - 1000
Type("1000 crystals has been removed from your inventory. You now have ", c.money, "crystals.")
print("")
weapon.append("Zeus' Armor")
Type("Zeus' Armor has been added to your inventory!")
print("")
armory()
elif "cancel" in armor:
shop()
else:
Type("The word or number you have entered is invalid. Please try again")
armory()
elif "cancel" in ask1:
inventory()
else:
Type("The number or word you have entered is invalid. Please try again.")
print("")
shop()
inventory()
答案 0 :(得分:4)
我认为你的问题在这里:
weapons = ["The sword of wizdom","The sword of kindness", "the sword of power", "the sword of elctricity", "the sword of fire", "the sword of wind", "the sword of ice", "the sword of self appreciation", "the sword of love", "the earth sword", "the sword of the universe"]
weaponAttack = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
c.weapon.append(weapons[w])
c.weapon.append(weaponAttack[w])
最后一行应该是:
c.weaponAttack.append(weaponAttack[w])