python中的while命令中的未知错误源

时间:2019-01-11 14:37:57

标签: python

我正在测试写在网站上的代码

http://foreverlearning.altervista.org/genetic-programming-symbolic-regression-pt-3/

该代码的一部分位于网页的底部。运行测试代码mainpova.py时,出现语法错误。

语法错误是

python mainprova4.py
Traceback (most recent call last):
File "mainprova4.py", line 1, in <module>
import generation as gn
File "/home/adam/DocumentsNew2/MathCode/SymbolicRegression/WebpageCode    /generation.py", line 46
while len(selectedMembers) < numMembers: indexSelected = 0 while     rnd.randint(0, 100) > int(pickProb * 100) and indexSelected !=     len(self.membersWithErrors) - 1:
^
SyntaxError: invalid syntax

指示符'^'实际上位于rnd.randint之前的while单词的e之下。这是代码的一部分,

def getMembersForReproduction(self, numMembers, pickProb):
""" Returns a certain number of distinct members from the    generation.
The first member is selected with probability pickProb. If it's not chosen, the 
second member is selected with probability pickProb, and so on. """
selectedMembers = []
while len(selectedMembers) < numMembers: indexSelected = 0 while   rnd.randint(0, 100) > int(pickProb * 100) and indexSelected != len(self.membersWithErrors) - 1:
        indexSelected += 1
    memberWithErrorSelected =   self.membersWithErrors[indexSelected]
    if memberWithErrorSelected[0] not in selectedMembers:
        selectedMembers.append(memberWithErrorSelected[0])
return selectedMembers

已针对较小和较大的错别字对源代码进行了更正。除此之外,除了while循环外,代码还不错。语法错误的根源是什么?该网站显示了输出结果。

2 个答案:

答案 0 :(得分:1)

格式仍然不正确。它应该看起来像这样:

def getMembersForReproduction(self, numMembers, pickProb):
    """ Returns a certain number of distinct members from the    generation.
    The first member is selected with probability pickProb. If it's not chosen, the 
    second member is selected with probability pickProb, and so on. """
    selectedMembers = []
    while len(selectedMembers) < numMembers:
        indexSelected = 0
        while rnd.randint(0, 100) > int(pickProb * 100) and indexSelected != len(self.membersWithErrors) - 1:
            indexSelected += 1
        memberWithErrorSelected =   self.membersWithErrors[indexSelected]
        if memberWithErrorSelected[0] not in selectedMembers:
            selectedMembers.append(memberWithErrorSelected[0])
    return selectedMembers

答案 1 :(得分:0)

我不知道缩进在python编码中起作用。 (我是python的新秀)。是的,它解决了问题,但又弹出了另一个问题。错误是

aanorbel@norbel-Aspire:~/Downloads$ oc login http://openshift.mydomain.com --token=6Q20qqF89FkZiQWPTLmOSLYANYQv9SmzbyXBkPAueJk
Logged into "http://openshift..mydomain.com:80" as "developer" using the token provided.

You have access to the following projects and can switch between them with 'oc project <projectname>':

  * default
    development
    jenkins
    kube-public
    kube-service-catalog
    kube-system
    management-infra
    openshift
    openshift-console
    openshift-infra
    openshift-logging
    openshift-metrics-server
    openshift-monitoring
    openshift-node
    openshift-sdn
    openshift-template-service-broker
    openshift-web-console
    test

Using project "default".
aanorbel@norbel-Aspire:~/Downloads$ oc whoami
Error from server (Forbidden): users.user.openshift.io "~" is forbidden: User "system:anonymous" cannot get users.user.openshift.io at the cluster scope: no RBAC policy matched
aanorbel@norbel-Aspire:~/Downloads$ 

此代码是

     python mainprova4.py
     Traceback (most recent call last):
     File "mainprova4.py", line 1, in <module>
     import generation as gn
     File "/home/adam/DocumentsNew2/MathCode/SymbolicRegression/WebpageCode /generation.py", line 105, in <module>
      for i in range(0, numCrossover):
      NameError: name 'numCrossover' is not defined

已经定义了numCrossover。我在这里想念什么?