Python错误令Python noobie感到困惑

时间:2013-10-06 14:04:14

标签: python

今天开始学习Python,我准备好了解我不理解的错误。

以下是我尝试运行脚本时遇到的错误:

enter image description here

这是有问题的剧本:

primes = []
x = raw_input('Enter the max value to check for primes: ')

for num in range(2, x+1):
    if len(primes) == 0:
        primes.append(num)
    else: 
        for prime in primes:
           if (num % prime == 0):
               break
        primes.append(num)

for number in primes:
    print number

从目前为止收到的错误来看,我看起来无法声明空白列表,并且它不喜欢我的输入法。我在他们工作的教程中或多或少地复制了这些行,我很困惑为什么他们在那里工作但不在这里。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:4)

您发布的错误消息以“未找到命令”开头,这意味着您不是在Python中运行此Python脚本,而是在shell中运行。要修复它,要么运行类似python Question7.py的内容,要么添加#!/usr/bin/env python作为脚本的第一行(这称为shebang行,并告诉解释器让Python运行脚本)

答案 1 :(得分:1)

我遇到的唯一问题是

TypeError: cannot concatenate 'str' and 'int' objects

通过以下方式解决此问题:

x = int(raw_input('Enter the max value to check for primes: '))