【问题标题】:I want to print each item from a list using a for loop but I get random numbers instead我想使用 for 循环从列表中打印每个项目,但我得到的是随机数
【发布时间】:2020-05-12 18:56:35
【问题描述】:

使用 for 循环,我想打印列表中的每个项目。所以我做了这个代码:

 from termcolor import colored # Ignore this and the "colored" function in the strings, just a 
                               # package I installed.
 titles = ["Dummy Story"] 

 bookshelf = {

 "Dummy Story": ["Page 1", "Page 2", "Page 3", "Page 4"] # The list I want it to print 

  } 


def readfun():

    print(colored("So you want to read a story now?", "green"))
    global titles        
    global bookshelf
    print(titles)
    storytoread = input(colored("Choose a book to read: ", "yellow"))
    for page in storytoread:
        print(page, end='')


readfun()

这是我正在制作的一种故事阅读项目。 storytoread 变量存储用户想要阅读的书名。所有信息均来自名为bookshelf 的字典。我希望它打印列表值的项目,因为storytoread 变量的值将被标识为字典bookshelf 中键值对的键。但是,它所做的只是打印存储在storytoread 中的字符串,而不是我希望它打印的字符串。我怎样才能做到这一点?

【问题讨论】:

  • 你知道你的书在书架上。你去书架找书了吗?
  • 嗯,你可以在标题中看到“虚拟故事”可用。所以我希望它是用户可以阅读的唯一一本书。

标签: python python-3.x dictionary


【解决方案1】:

变化:

storytoread = input(colored("Choose a book to read: ", "yellow"))
for page in storytoread:
    print(page, end='')

收件人:

storytoread = input(colored("Choose a book to read: ", "yellow"))
for page in bookshelf[storytoread]:
    print(page, end='')

请记住,如果输入在bookshelf 中不作为键存在,您将得到KeyError 异常。为避免这种情况,您可以先检查该书是否存在于 bookshelf 字典中:

if storytoread in bookshelf: 
    for page in bookshelf[storytoread]:
        print(page, end='')
else:
    print("Book not found: ", storytoread)

【讨论】:

    【解决方案2】:

    我建议使用单个变量 BOOKSHELF 包含有关您的书籍的所有信息。你也不需要使用global:

    from termcolor import colored
    
    BOOKSHELF = {'Dummy Story': ['Page 1', 'Page 2', 'Page 3', 'Page 4']}
    
    def read_fun():
    
        print(colored('So you want to read a story now?', 'green'))
        print(list(BOOKSHELF.keys()))
        story_to_read = input(colored('Choose a book to read: ', 'yellow'))
        if story_to_read in BOOKSHELF:
            print(', '.join(BOOKSHELF[story_to_read]))
        else:
            print(colored('Book not found!', 'red'))
    
    read_fun()
    

    样本输出:

    【讨论】:

      【解决方案3】:

      你的书名是字典,所以你应该把它作为键bookshelf[book_name]

      from termcolor import colored # Ignore this and the "colored" function in the strings, just a 
                                     # package I installed.
       titles = ["Dummy Story"] 
      
       bookshelf = {
      
       "Dummy Story": ["Page 1", "Page 2", "Page 3", "Page 4"] # The list I want it to print 
      
        } 
      
      
      def readfun():
      
          print(colored("So you want to read a story now?", "green"))
          global titles        
          global bookshelf
          print(titles)
          storytoread = input(colored("Choose a book to read: ", "yellow"))
          for page in bookshelf[storytoread]:
              print(page, end='')
      
      
      readfun()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-29
        • 2022-07-26
        • 2021-07-21
        • 1970-01-01
        相关资源
        最近更新 更多