所以我對python很陌生,目前正在制作一個基于文本的游戲。我想給玩家隨機生成的代碼片段,然后要求他們輸入正確的組合。我將這些數字存盤在一個串列中,將它們打亂并將它們連接在一起,以從四個數字的串列中以隨機順序獲得一個四位數字。由于數字是隨機的,我不知道組合,所以我不能要求確切的四位數字。我試圖使用戶輸入等于我存盤四位數的變數,但它不起作用。它要么無法識別正確的數字,要么說“int”物件不可呼叫。有人可以幫忙嗎?
coderan = random.sample(safecode, len(safecode))
def convert(coderan):
num = [str(n) for n in coderan]
code = int("".join(num))
return code
compcode = (convert(coderan))
print(type(compcode))
print(compcode)
c2 = int(input())
time.sleep(1)
ans = 'incorrect'
while(ans=='incorrect'):
if(c2()==compcode):
time.sleep(1)
print(SCENE6COMP)
ans = 'correct'
else:
time.sleep(1)
print("That's not the right combination. Try again! ")
c2 = input()
uj5u.com熱心網友回復:
您在 if 陳述句條件中犯了一個錯誤。隨著c2()您呼叫存盤在 c2 中的 int-object (您的錯誤訊息指出)。要解決此問題,您的 if 陳述句應如下所示:
if(c2==compcode):
…
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487142.html
