我有一个代码,需要以不同的顺序打印出来,但它一直在括号中打印出来

所以这是代码,我需要以不同的方式打印输出并且不带括号。我怎么做?


    phonebook = {}

    n=1

    line = str(input('Name and clour: '))

    while line:

      name, colour = line.split()

      phonebook[n] = name, colour

      line = str(input('Name and clour: '))

      n=n+1

    print(phonebook[3])

    print(phonebook[1])

    print(phonebook[2])


饮歌长啸
浏览 74回答 1
1回答

www说

你的 while 条件True总是如此,程序不断要求 name和colour。因为,最高的数字key是3,所以我将条件更改为n<4。因此,只有当数字在phonebook[key]is中时,该程序才能正常工作key=3。phonebook = {}n=1#line = str(input('Name and clour: '))#while line:while n<4:&nbsp; &nbsp; line = str(input('Name and clour: '))&nbsp; &nbsp; name, colour = line.split()&nbsp; &nbsp; phonebook[n] = name, colour&nbsp; &nbsp; n=n+1print(phonebook[3])print(phonebook[1])print(phonebook[2])还,keys = [1,2,3]for k in keys:&nbsp; &nbsp; name, colours = phonebook[k]&nbsp; &nbsp; print(name, colour)这会打印不带括号的名称和颜色
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python