首頁>Program>source

我的代碼在Test.py檔案中

name = input("enter your name :")
id = int(input("enter your id :")
score = [int]*3
MyLib.calculate(score)
MyLib.result(name, id, score)

這是MyLib.py中的代碼

def calculate (x) :
    n = 0
    for n in x :
        nilai = int(input("Input Your Score : "))
def result (a,b,c) :
    n = 0
    sum = 0
    for n in c :
        if n >= 80 and n <= 100 :
            i = 4
            x = "A" 
        elif n < 80 and n >= 70 :
            i = 3
            x = "B"
        elif n < 70 and n >= 60 :
            i = 2
            x = "C"
        elif n < 60 and n >= 50 :
            i = 1
            x = "D"
        elif n < 50 and n >= 0 :
            i = 0
            x = "E"
        sum += i
    print("Your Name :",a)
    print("Your ID :", b)
    print("Your score number is ",n," and your score alphabet is ",x)
    print("Your avg score is", sum)

但是在我輸入分數(例如80、70、60)之後,結果是TypeError:'type'和'int'的例項之間不支援'>='。

最新回復
  • 7月前
    1 #

    score = [int]*3
    

    产生列表

    [int, int, int]
    

    当您通過 scoreMyLib.calculate ,您需要遍歷每个元素(即每个 int )並建立本地變數 nilai 包含使用者輸入的值。

    但是, nilai 被重新分配给每个新輸入,這意味着您丢失了先前的值.此外,您永远不会返迴任何這些值,這意味着它们会永远丢失。

    score 然後傳遞给 result .然後將其迭代.在每次迭代中, n 等於 int 因為那是列表中的唯一元素。

    您比较 int 到80行

    if n >= 80 and n <= 100:
    

    inttype型別的物件 .比较 type没有任何意義 設為80的整數。

    尝試更換 MyLib.calculate 与以下內容。

    def calculate(length):
        scores = []
        for _ in range(length):
            nilai = int(input("Input Your Score : "))
            scores.append(nilai)
        return scores
    

    然後您可以致電

    scores = MyLib.calculate(3)
    

  • python 3.x:使用網站阻止功能在Python3中建立DNS服務器吗?
  • javascript:快速滑鼠悬停時的動画錯誤