简体   繁体   English

我在 python 中制作了一个随机动漫选择器,我似乎无法找到错误所在。有人可以帮我解决这个项目吗

[英]I was making a random anime selector in python and I can't seem to get where the error is.Could somebody help me out with this project

I have tried searching for a solution but haven't found an answer yet我已尝试寻找解决方案,但尚未找到答案

from random import choice
#create a list of animes and insert their genres

Myanimes= {

   "Action":["One Punch Man","Demon Slayer","Vinland Saga","Baki","Baki Hanma","Full Metal alchemist:brotherhood","Yasuke","Dragon Ball Z","Dragon Ball Super"],
   
   "Adventure":["My hero academia "," Black Clover","Naruto","Cowboy Bebop"],
   
   "Supernatural":["Blue exorcist","Jojo's Bizarre Adventure"],
   
   "Sci-fi":["Attack on Titan","Assasination Classroom","Parasyte"],
   
   "Fantasy":["Jujustsu Kaisen","Hunter x Hunter","Devilman Crybaby"],
   
   "Comedy":["Komi Can't Communicate"],
   
   "Drama":["Food Wars","The Disastrous Life of Saiki K"],
   
   "Psychological":["Code Geass","Mob Pyscho","Death Note"],
   
   "Horror":["Castlevania","Record of Ragnarok"],



   
}
print(choice(Myanimes))

Error:错误:

KeyError: 4

I keep getting a key error 4 but if you find any other errors please help me correct them.我不断收到一个关键错误 4,但如果您发现任何其他错误,请帮助我更正它们。

As said in the comments, random.choice works with lists and not with dicts.正如评论中所说, random.choice适用于列表而不是字典。 You have two options:你有两个选择:

  1. Use the list of the keys of the dict:使用字典的键列表:
genre=choice(list(Myanimes.keys()))
print(genre, Myanimes[genre])
# Result: Fantasy ['Jujustsu Kaisen', 'Hunter x Hunter', 'Devilman Crybaby']
  1. Use the list of items, which are (key, value) pairs使用项目列表,它们是(key, value)
print(choice(list(Myanimes.items())))
# Result: ('Supernatural', ['Blue exorcist', "Jojo's Bizarre Adventure"])

如果为 0,有人可以帮我解决这个错误吗<upperpipes[0]['x']<5: keyerror: 0< div><div id="text_translate"><p> 这是代码:-我正面临与upperpipes的问题。 要么它无法访问上管的第一个元素的upperpipes ,即'x' ,要么我无法理解。 帮我解决这个错误</p><pre class="lang-py prettyprint-override">def mainGame(): upperpipes=[ {'x': SCREENWIDTH + 200, 'y': newPipe1[0]['y']}, {'x': SCREENWIDTH + 200 + (SCREENWIDTH / 2), 'y': newPipe2[0]['y']} ] lowerpipes = [ {'x': SCREENWIDTH + 200, 'y': newPipe1[1]['y']}, {'x': SCREENWIDTH + 200 + (SCREENWIDTH / 2), 'y': newPipe2[1]['y']} ] #Add A NEW PIPE WHEN THE fIRST IS ABOUT TO cross THE LEFT MOST PART OF SCREEN if 0&lt;upperpipes[0]['x']&lt;5: newpipe=getRandomPipe() upperpipes.append(newpipe[0]) lowerpipes.append(newpipe[1])</pre> </div></upperpipes[0]['x']<5:> - Can somebody help me out in this error if 0<upperpipes[0]['x']<5: KeyError: 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以帮我找出代码中的错误吗? (Python 基本图形用户界面) - Could somebody help me figure out the error in my code? (Python basic GUI) 如果为 0,有人可以帮我解决这个错误吗<upperpipes[0]['x']<5: keyerror: 0< div><div id="text_translate"><p> 这是代码:-我正面临与upperpipes的问题。 要么它无法访问上管的第一个元素的upperpipes ,即'x' ,要么我无法理解。 帮我解决这个错误</p><pre class="lang-py prettyprint-override">def mainGame(): upperpipes=[ {'x': SCREENWIDTH + 200, 'y': newPipe1[0]['y']}, {'x': SCREENWIDTH + 200 + (SCREENWIDTH / 2), 'y': newPipe2[0]['y']} ] lowerpipes = [ {'x': SCREENWIDTH + 200, 'y': newPipe1[1]['y']}, {'x': SCREENWIDTH + 200 + (SCREENWIDTH / 2), 'y': newPipe2[1]['y']} ] #Add A NEW PIPE WHEN THE fIRST IS ABOUT TO cross THE LEFT MOST PART OF SCREEN if 0&lt;upperpipes[0]['x']&lt;5: newpipe=getRandomPipe() upperpipes.append(newpipe[0]) lowerpipes.append(newpipe[1])</pre> </div></upperpipes[0]['x']<5:> - Can somebody help me out in this error if 0<upperpipes[0]['x']<5: KeyError: 0 我似乎无法摆脱这个程序在 python 上的循环 - I can't seem to get out of the loop on this program on python tensorflow 导入错误有人可以帮我吗 - tensorflow import error can somebody help me 我正在通过字典建立关系,但是有人可以帮我解决一个我不知道的输出吗? - I am making relationships from a dictionary, but can someone help me fix one output I can't figure out? 我正在尝试使用 python 3.8 获取日语句子。 但我只是得到了无法理解的字符,有人可以帮助我吗? - I'm trying to get japanese sentences with python 3.8. but I'm just gettting non-intelligible characters, somebody can help me? Python项目小问题我似乎无法找出要打印的东西 - Python project little issue I can't seem to figure out to print something 您好,我是python新手,在将文件作为输入时遇到困难。 有人可以帮我吗? - Hello, I am new to python and I am having difficulties on having a file as an input. Can somebody help me please? 运行此 python 代码时出现错误。 错误是“ElementNotInteractableException”。 任何人都可以帮助我吗? - I am getting error while running this python code. The error is “ElementNotInteractableException”. Can any on help me out? Pandas python 帮助 - 似乎无法获得代码来做我需要的事情 - Pandas python help - can't seem to get the code to do what I need it to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM