为什么会出现这种类型错误?

2024-09-27 07:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我相信这个错误意味着我不能在一个循环中包含一个变量,但是我正在努力寻找解决方法。。。。你知道吗

错误是

TypeError: range() integer end argument expected, got unicode.

这本书想问我的问题是:

Try wring a program the will prompt for an number and print the correct times table (up to 12).

这是我的密码:

def main():
    pass    
choice = raw_input("Which times table would you like")    
print ("This is the", choice , "'s times table to 12")    

var1 = choice*12 + 1

for loopCounter in range (0,var1,choice):
    print(loopCounter)    

if __name__ == '__main__':
    main()

有什么建议吗?提前谢谢。你知道吗


Tags: theto方法formain错误tablerange
3条回答

raw_input函数给您一个字符串,不是整数。如果您希望它是一个整数(例如,如果您希望将它乘以12或在range调用中使用它),则需要如下内容:

choice = int(raw_input("Which times table would you like"))

这种简单化的解决方案存在潜在的问题(例如,当您输入的是而不是一个数字时会发生什么),但这应该足以解决您当前的问题。你知道吗

您的程序将运行一些更改。你知道吗

def main():
    pass    
choice = input("Which times table would you like")    
print ("This is the " + choice + "'s times table to 12")    

var1 = int(choice)*12 + 1

for loopCounter in range (0,var1,int(choice)):
    print(loopCounter)    

if __name__ == '__main__':
    main()

现在您可能需要对其进行更多的调整,以便获得正确的输出,上面的代码将编译并运行。你知道吗

这个错误仅仅意味着它在假定整数时得到了一个unicode值。 这是因为使用raw_input表示choice。你知道吗

编辑:raw_input不解释您的输入。input是的。你知道吗

相关问题 更多 >

    热门问题