簡體   English   中英

我不斷收到類型錯誤,不知道為什么

[英]I keep getting a type error and don't know why

def printWord(a,b):
    a= raw_input ("What would you like me to say?")
    b= raw_input ("How many times would you like me to say it?")
    int(float(b))
    for i in range(b):
        print a

此代碼不斷給我這個錯誤:

line 10, in printWord
  for i in range(b):
TypeError: range() integer end argument expected, got str.

您對這一行有正確的想法:

int(float(b))

但這不會改變b就地。 您必須保留結果。 用這個:

b = int(float(b))

調用int(float(b))不會更改b的狀態。 在該行之后,b仍然是字符串,而range()需要一個整數。 我可能會將該行更改為b = int(b)以將b修改為所需的值。

暫無
暫無

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

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