Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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 如何使用repr()的多个实例_Python_Repr - Fatal编程技术网

Python 如何使用repr()的多个实例

Python 如何使用repr()的多个实例,python,repr,Python,Repr,所以我让我的程序打印出我想要的第一部分,但是现在对于第二个菜单选项,我希望它按名称查找Pokemon,然后专门为该Pokemon填写pokedex条目。我的想法是使用repr()的多个实例,但到目前为止,这并没有真正对我起作用 编辑:我决定尝试使用以下搜索方法: def lookup_by_name(pokedex, name): for i in pokedex: search: name

所以我让我的程序打印出我想要的第一部分,但是现在对于第二个菜单选项,我希望它按名称查找Pokemon,然后专门为该Pokemon填写pokedex条目。我的想法是使用repr()的多个实例,但到目前为止,这并没有真正对我起作用

编辑:我决定尝试使用以下搜索方法:

def lookup_by_name(pokedex, name):
            for i in pokedex:
                    search: name
                    if i == search:
                            print(i)
                            print("yes")
但当我这样做时,我会得到错误“赋值前引用的局部变量'search'”

导入字符串
def打印菜单():
打印(“1.打印Pokedex”)
打印(“2.按名称打印口袋妖怪”)
打印(“3.按数字打印口袋妖怪”)
打印(“4.用字体数口袋妖怪”)
打印(“5.打印口袋妖怪的平均战斗点数”)
打印(“6.退出”)
返回打印菜单
def打印_pokedex(pokedex):
打印(pokedex)
按名称查找def(pokedex,名称):
打印(口袋妖怪。打印口袋妖怪的名字(口袋妖怪))
口袋妖怪类:
定义初始(自我、姓名、编号、战斗点数、类型):
self.number=number
self.name=名称
自我战斗点数=战斗点数
self.types=类型
定义报告(自我):
return“Number:{},Name:{},CP:{},Types:{},\n.格式(str(self.Number),self.Name,str(self.batch_points),str(self.Types).strip(“[]”,“”)
def print_pokemon_name(self):
return“Number:{},Name:{},CP:{},Types:{},\n.格式(str(self.Number),self.Name,str(self.batch_points),str(self.Types).strip(“[]”,“”)
# ---------------------------------------
#请勿更改此线以下的任何内容
# ---------------------------------------
def create_pokedex(文件名):
pokedex=[]
文件=打开(文件名为“r”)
对于文件中的口袋妖怪:
pokelist=pokemon.strip().split(“,”)
number=int(pokelist[0])#number
name=pokelist[1]#name
战斗点数=智力(神奇宝贝[2])#生命值
类型=[]
对于范围内的位置(3,len(pokelist)):
类型+=[pokelist[位置]]#类型
pokedex+=[口袋妖怪(名称、编号、战斗点数、类型)]
file.close()文件
返回pokedex
# ---------------------------------------
def get_选项(低、高、消息):
合法选择=错误
虽然不是合法的选择:
合法选择=正确
回答=输入(信息)
对于回答中的字符:
如果字符不在string.digits中:
合法选择=错误
打印(“这不是一个数字,请重试。”)
打破
如果是合法选择:
答案=int(答案)
如果(回答<低)或(回答>高):
合法选择=错误
打印(“这不是一个有效的选择,请重试。”)
回覆
# ---------------------------------------
def main():
pokedex=create_pokedex(“pokedex.txt”)
选择=0
而选择!=6:
打印菜单()
choice=get_选项(1,6,“输入菜单选项:”)
如果选项==1:
打印pokedex(pokedex)
elif选项==2:
name=input(“输入口袋妖怪名称:”).lower()
按名称查找(pokedex,名称)
elif选项==3:
number=get_选项(11000,“输入口袋妖怪编号:”)
按编号查找(pokedex,编号)
elif选项==4:
口袋妖怪类型=输入(“输入口袋妖怪类型:”).lower()
按类型划分的总数(pokedex、pokemon)
elif选项==5:
平均命中率(pokedex)
elif选项==6:
打印(“谢谢,再见!”)
打印()
# ---------------------------------------
main()

Hi@CrockPocket。欢迎来到SO。为什么要创建一个新变量“search”。相反,你可以直接比较(i==name)?你的代码中有很多令人困惑的东西,但是我猜如果i.name==name,你可能想要
?@Venkat\u The_Mufasa你是对的,我在那里添加了不必要的东西,谢谢@Blckknght这是完全正确的方法,谢谢!
import string
def print_menu():
        print("1. Print Pokedex")
        print("2. Print Pokemon by Name")
        print("3. Print Pokemon by Number")
        print("4. Count Pokemon with Type")
        print("5. Print Average Pokemon Combat Points")
        print("6. Quit")


        return print_menu
def print_pokedex(pokedex):
        print(pokedex)
def lookup_by_name(pokedex, name):
        print(Pokemon.print_pokemon_name(Pokemon))


class Pokemon:
        def __init__(self, name, number, combat_points, types):
                self.number = number
                self.name = name
                self.combat_points = combat_points
                self.types = types

        def __repr__(self):
                return "Number: {}, Name: {}, CP: {}, Types: {}, \n".format(str(self.number), self.name, str(self.combat_points), str(self.types).strip("[]',"))

        def print_pokemon_name(self):
                return "Number: {}, Name: {}, CP: {}, Types: {}, \n".format(str(self.number), self.name, str(self.combat_points), str(self.types).strip("[]',"))







# ---------------------------------------
# Do not change anything below this line
# ---------------------------------------

def create_pokedex(filename):
    pokedex = []
    file = open(filename, "r")

    for pokemon in file:
        pokelist = pokemon.strip().split(",")
        number = int(pokelist[0])               # number
        name = pokelist[1]                      # name
        combat_points = int(pokelist[2])        # hit points
        types = []
        for position in range(3, len(pokelist)):
            types += [pokelist[position]]       # type
        pokedex += [Pokemon(name, number, combat_points, types)]

    file.close()
    return pokedex

# ---------------------------------------

def get_choice(low, high, message):
    legal_choice = False
    while not legal_choice:
        legal_choice = True
        answer = input(message)
        for character in answer:
            if character not in string.digits:
                legal_choice = False
                print("That is not a number, try again.")
                break 
        if legal_choice:
            answer = int(answer)
            if (answer < low) or (answer > high):
                legal_choice = False
                print("That is not a valid choice, try again.")
    return answer

# ---------------------------------------

def main():
    pokedex = create_pokedex("pokedex.txt")
    choice = 0
    while choice != 6:
        print_menu()
        choice = get_choice(1, 6, "Enter a menu option: ")
        if choice == 1:    
            print_pokedex(pokedex)
        elif choice == 2:
            name = input("Enter a Pokemon name: ").lower()
            lookup_by_name(pokedex, name)
        elif choice == 3:
            number = get_choice(1, 1000, "Enter a Pokemon number: ")
            lookup_by_number(pokedex, number)
        elif choice == 4:
            pokemon_type = input("Enter a Pokemon type: ").lower()
            total_by_type(pokedex, pokemon_type)
        elif choice == 5:
            average_hit_points(pokedex)
        elif choice == 6:
            print("Thank you.  Goodbye!")
        print()

# ---------------------------------------

main()