【问题标题】:Is there any solution that this computer recognize the list but not as a integer?是否有任何解决方案可以让这台计算机识别列表但不能识别为整数?
【发布时间】:2021-11-22 12:29:11
【问题描述】:

目标:分别找到每个科目的最佳得分手。分数在整个 int(input()) 中接收。

示例
数学:[0 20 30 40 50 ] 输出:“最佳分数:50”

英文:[0 40 50 80 90 ] 输出:“Best Score: 80”

科学:[0 90 78 90 97] 输出:“最佳分数:97”

问题:即使我使用了 copy() 或 list[:],计算机仍将复制的代码识别为整数对象,而不是列表。

输入代码:完全正确

问题所在的代码:

   subject_score = [0, 0, 0, 0, 0]
   a, b, c = subject_score[:]
   i = 0
   for subject in midterm_score:
       for i in range(0, len(subject_score) - 1):
           a[i] = midterm_score[0][i]
           b[i] = midterm_score[1][i]
           c[i] = midterm_score[2][i]
       minscore1 = a[0]
       for i in range(1, len(subject_score) - 1):
           if minscore1 < a[i]:
               minscore1 = a[i]
       minscore2 = b[0]
       for i in range(1, len(subject_score) - 1):
           if minscore2 < b[i]:
               minscore2 = b[i]
       minscore3 = c[0]
       for i in range(1, len(subject_score) - 1):
           if minscore3 < c[i]:
               minscore3 = c[i]
   print(a, b, c)
   print("Math:"+ minscore1)
   print("English:" + minscore2)
   print("Science:"+ minscore3)

出现问题的地方:

    subject_score = [0, 0, 0, 0, 0]
    a, b, c = subject_score[:]

程序员的能力:菜鸟

回答者:会有美好的一天

谢谢。

【问题讨论】:

  • 您希望变量 a、b 和 c 包含什么? [0,0,0,0,0]?
  • 首先,在将 subject_score 分配给 a、b 和 c 时,您应该得到一个 valueError。如果要将 subject_score 分配给 a、b 和 c,以便 a、b 和 c 成为列表,则必须单独执行。类似于 a = score[:];b = score[:];c=score[:]

标签: python list integer traceback


【解决方案1】:

发生这种情况的原因是python使用了赋值解包。

your_list = [0,1,2]
a, b, c = your_list

在此示例中,变量将与列表不同,但列表的元素将被解包。这实际上是结果:

a = 0
b = 1
c = 2

如果你想让每个变量都和your_list一样,你可以这样做:

a = your_list
b = your_list 
c = your_list

或者你可以这样做:

a, b, c = [your_list for i in range(x)]

记得用 x 替换你想要的变量数量

【讨论】:

    猜你喜欢
    • 2014-05-03
    • 2018-06-03
    • 2011-02-21
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多