簡體   English   中英

我的 python 代碼出現錯誤,你能幫我修復它嗎?

[英]i am getting an error in my python code can you help me fix it?

我收到錯誤消息:

    randint = randint(1, 2)
TypeError: 'int' object is not callable

這是我的代碼。 我不知道是什么導致了這種情況發生,這是否與循環兩次有關,因為它允許我回答第一個問題然后失敗: screenshot

from random import randint

score = 0

print('\n\n\n\nyour score right now is ', score, 'if it drops below zero you lose')

def make_question(num, italian, answer):

    global score

    if randint == int(num):
        a = input('\nWhat does ' + str(italian) + ' mean? ')
        if a == str(answer):
            score += 1
            print('\ncorrect your score is: ', score)
        else:
            score -= 1
            print('\nincorrect your score is now: ', score)


while True:
    randint = randint(1, 2)
    make_question(1, 'ripetere', 'repeat')
    make_question(2, 'puoi/potere ripetere', 'can you repeat that')

你第一次這樣做:

randint = randint(1, 2)

randint是來自random模塊的函數,並且調用有效。 然后你用12替換它,現在它只是一個int 為您生成的值使用不同的名稱(例如, whichquestion = randint(1, 2)並替換if randint == int(num):if whichquestion == int(num): ),所以你不丟棄您需要的功能的句柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM