我的变量总是出现语法错误。我该怎么办

2024-09-30 01:28:27 发布

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

变量x总是显示为语法错误。它表示要生成的质数的个数

    x=501
    while x<1 or x>500:
        NoNos=int(input("Number of Prime Numbers"))
        if x<1:
            print("The number has to be greater than 1")
        if x>500:
            print("The number has to be lesser than 500")
    PrimeNo=2
    PrimeNos=[]
    While not x==0:
        if PrimeNo==2:
            PrimeNos=PrimeNos+[PrimeNo]
            x=x-1
            PrimeNo=PrimeNo+1
            continue
        for divisor in the range (2,PrimeNo-1):
            if not PrimeNo%divisor=0:
                x=x-1
                PrimeNos=PrimeNos+[PrimeNo]
    print(PrimeNos)

Tags: thetonumberifnotbedivisor质数
2条回答

我修正了你代码中的一些错误

  1. While=>;While
  2. 范围=>范围
  3. PrimeNo%除数=0:=>;PrimeNo%除数==0:

==============================================================================

x=501
while x<1 or x>500:
    NoNos=int(input("Number of Prime Numbers"))
    if x<1:
        print("The number has to be greater than 1")
    if x>500:
        print("The number has to be lesser than 500")
PrimeNo=2
PrimeNos=[]
while not x==0:
    if PrimeNo==2:
        PrimeNos=PrimeNos+[PrimeNo]
        x=x-1
        PrimeNo=PrimeNo+1
        continue
    for divisor in range (2,PrimeNo-1):
        if not PrimeNo%divisor == 0:
            x=x-1
            PrimeNos=PrimeNos+[PrimeNo]
print(PrimeNos)
$ python test.py
  File "test.py", line 11
    While not x==0:
              ^
SyntaxError: invalid syntax

这是Python中的一个bug。插入符号应指向“While”中的大写字母“W”。你必须用所有小写字母拼写“while”。在

你还有一些其他的错误:

^{pr2}$

这里的插入符号应该指向“the”,它应该被删除。在

  File "test.py", line 18
    if not PrimeNo%divisor=0:
                          ^
SyntaxError: invalid syntax

这一次插入符号是在正确的地方='必须是'=='。在

在我做了所有这些更改之后,你的程序仍然不能工作,但是剩下的问题似乎不是语法问题。在

(我把http://bugs.python.org/issue23518放错了插入符号上。)

相关问题 更多 >

    热门问题