Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建随机字符的RPG字符模拟器_Python_Python 3.x - Fatal编程技术网

Python 创建随机字符的RPG字符模拟器

Python 创建随机字符的RPG字符模拟器,python,python-3.x,Python,Python 3.x,我目前正在做一个高级计算机科学课程,并且被分配了一个任务,其中包括编写一些代码,从子类中随机生成10个生物,添加到一个列表中,并将它们保存到一个文件中,以便以后打印和编辑。在做了大量研究之后,我已经用Python编写了一些代码,但无法让它生成任何随机的生物。我对编写代码非常陌生,但我确实想学习,所以如果有人能指出我的错误所在,我将非常感激。请看下面我的代码。非常感谢 import textwrap from random import randint global randomness glob

我目前正在做一个高级计算机科学课程,并且被分配了一个任务,其中包括编写一些代码,从子类中随机生成10个生物,添加到一个列表中,并将它们保存到一个文件中,以便以后打印和编辑。在做了大量研究之后,我已经用Python编写了一些代码,但无法让它生成任何随机的生物。我对编写代码非常陌生,但我确实想学习,所以如果有人能指出我的错误所在,我将非常感激。请看下面我的代码。非常感谢

import textwrap
from random import randint
global randomness
global messages
import random
name = 'Barbarian', 'Elf', 'Wizard', 'Dragon', 'Knight'
syllables = [ 'Ga',  'Bu', 'Zo', 'Meu' ]

#to create generic character class
class Creature:
 def __init__(self, name, type, health=100, power=50, sap=20, speed=50):
  self.name = name
  self.type = type
  self.health = health
  self.power = power
  self.sap = sap
  self.speed = speed

def __str__(self):
 return textwrap.dedent(f"""\
Name: {self.name}
Type: {self.type}
Health: {self.health}
Power {self.power}
Sap {self.sap}
Speed {self.speed}\n""")

# to create creature subclasses

class Barbarian(Creature):
 def __init__(self, name, type="Barbarian", health=100, power=70, sap=20,            speed=50, **kwargs):
  super().__init__(name=name, type=type, health=health, power=power,   sap=sap, speed=speed, **kwargs)

def __str__(self):
 return super().__str__()

class Elf(Creature):
 def __init__(self, name, type= "Elf", health=100, power=30, sap= 60,   speed=60,**kwargs):
  super().__init__(name=name, type=type, health=health, power=power, sap=sap, speed=speed, **kwargs)

def __str__(self):
 return super().__str__()

class Wizard(Creature):
 def __init__(self, name, type= "Wizard", health=100, power=50, sap= 70, speed=30,**kwargs):
   super().__init__(name=name, type=type, health=health, power=power, sap=sap, speed=speed, **kwargs)

def __str__(self):
 return super().__str__()

class Dragon(Creature):
 def __init__(self, name, type= "Dragon", health=100, power=90, sap= 40,    speed=50,**kwargs):
  super().__init__(name=name, type=type, health=health, power=power,   sap=sap, speed=speed, **kwargs)

def __str__(self):
 return super().__str__()

class Knight(Creature):

 def __init__(self, name, type= "Knight", health=100, power=60, sap= 10, speed=60,**kwargs):
  super().__init__(name=name, type=type, health=health, power=power, sap=sap, speed=speed, **kwargs)

def __str__(self):
 return super().__str__()

def mainmenu():
 print("1:name generator\n2:generate creatures\n3:print      creaturelist\n4:edit creaturelist")
 choice=input()
 return choice

choice = mainmenu()
while (choice != "1" and choice !="2" and choice !="3" and choice !="4"):
 choice = mainmenu()

if choice == "1":
  name = syllables[ random.randint( 0, 3 )], syllables[ random.randint( 0, 3 )], syllables[ random.randint( 0, 3 )]

print (name)
f = open("creaturelist.txt","w") #to open file and append name of creature
f.close() # to close file

if choice == "2":
# pick ten random creatures then store in a file
 @classmethod
 def get_random_instances(cls):
  return random.sample(cls.Creature, 10)
  f = open("creaturelist.txt","w") #to open text file and write list of creatures to it
  f.close() # to close text file

# to print creature list to the console
if choice == "3":
  with open("creaturelist.txt") as file:
   print (f.read)
   f.close()

# to open and write to text file to edit creaturelist and stats as required
if choice == "4":
  f = open("creaturelist.txt","w") #to open text file and write to file as required
  f.close()
对于“选择1”,可以这样做:

if choice == "1":
  name = syllables[ random.randint( 0, 3 )], syllables[ random.randint( 0,     3 )], syllables[ random.randint( 0, 3 )]
  print (name)
  f = open("creaturelist.txt","w") #to open file and append name of creature
  f.write("".join(name))
  f.close() # to close file
print()
在终端上显示字符串,而不是在文件中(在写入文件之前,必须先打开文件)

对于“选择2”,这将为您提供10个生物名称:

if choice == "2":
  print(random.choices(name, k=10))
“选择3”部分最好重写如下:

if choice == "3":
  f = open("creaturelist.txt")
  print(f.read())
  f.close()
与代码中的另一部分相比,您将
f=open()
与open()混合作为文件
,然后您就迷路了(混合
f
文件


关于你的代码还有很多要说的…

我可以看一下打印的输出吗(random.sample(cls.biote,10))
def\uu str(self):return super()。\uu str\uuuu()
是不必要的,因为子类继承了方法。也许你想用“选择2”做的是一个“工厂方法模式”:?请检查你的缩进!按照python标准,每个缩进应该有4个空格。另一个一般性的注意是,应该避免使用
type
作为变量的名称,它在python中是一个保留字!非常感谢大家的评论;我真的很感谢你的帮助。这是打印输出(随机样本(cls.Biotel,10)):“野蛮人”、“精灵”、“巫师”、“龙”、“骑士”)