【问题标题】:Why is 'None' in the output? [duplicate]为什么输出中有“无”? [复制]
【发布时间】:2022-09-29 22:36:11
【问题描述】:

我有这个问题。

ClassA1=[7.8,5.6,8.7,8.9,9,9.5]
ClassA2=[6.0,6.5,9.3,9.2,7.5]
choice=int((input(\"Press 1 if you want to sort the scoreboard in ascending order.\\n. Press 2 if you want to sort the scoreboard in descending order.\"))
total = lopA1+lopA2 
if choice == 1:
  print(total.sort())
elif choice == 2:
  print(total.sort(reverse=True))

运行后我输入 1 或 2

None
  • total.sort() 对列表进行就地排序并返回无。你想要total.sort(),然后是print(total)

标签: python list


【解决方案1】:

这些是 list.sort 函数 sort(*, key: None = ..., reverse: bool = ...) -> Nonesort(*, key: (Any) -> SupportsRichComparison, reverse: bool = ...) -> None 的签名。它仅对列表进行排序(不返回)。这应该起作用:

ClassA1=[7.8,5.6,8.7,8.9,9,9.5]
ClassA2=[6.0,6.5,9.3,9.2,7.5]
choice=int(input("Press 1 if you want to sort the scoreboard in ascending order.\n. Press 2 if you want to sort the scoreboard in descending order."))
total = ClassA1+ClassA2 
if choice == 1:
  total.sort()
elif choice == 2:
  total.sort(reverse=True)
print(total)

【讨论】:

    猜你喜欢
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多