简体   繁体   English

如何制作嵌套循环语句以返回到特定语句

[英]How to craft a nested loop statement to go back to a certain statement

Hello I am having trouble with crafting this loop instead of it going back to line after I declared the "nameone" variable I want it to go back to either input '*' or a name I tried putting the loop before the input but it will come up with the error "referenced before assignment" which I understand why but I can't figure out a way to craft it to where it will let you enter a new name 您好,我在设计此循环时遇到麻烦,而不是在声明“ nameone”变量后返回到行,我希望它返回输入“ *”或我尝试将循环放在输入之前的名称,但是它将提出了“分配前已被引用”的错误,我理解为什么会出现这种错误,但我想不出办法将其制作到可以让您输入新名称的位置

def GetPosInt():

        nameone = str(input("Please enter a student name or '*' to finish: "))

        while nameone != "*":
            scoreone = int(input("Please enter a score for " + nameone +": "))

            if scoreone < 0:
                print("positive integers please!")
                break

            else:
                scoretwo = float(input("Please enter another score for "+nameone+": "))
                scorethree = float(input("Please enter another score for "+nameone+": "))

            testscores = scoreone + scoretwo + scorethree    
            avg = testscores / 3    
            print("The average score for",nameone,"is ",avg)

        if nameone == "*":
            print("no bueno")

main 主要

def main():
    GetPosInt()

" ...it will come up with the error "referenced before assignment"[...] ...它将出现错误”分配前已引用“ [...]

This is because, you're likely specifying the condition to be while nameone != '*': but, not declaring nameone until you enter the loop. 这是因为,您可能将条件指定为while nameone != '*':但是在进入循环之前不声明nameone You can remedy this easily. 您可以轻松地对此进行补救。 Try this: 尝试这个:

nameone = None

while nameone != "*":
    nameone = str(input("Please enter a student name or '*' to finish: "))

    ... # everything else remains the same

    if nameone == "*":
        print("no bueno")

If what I understand of your question is correct then what you need is an outer loop. 如果我对您的问题的理解是正确的,那么您需要的是外部循环。

Something like: 就像是:

cont = 'y'

while cont=='y':
    # Do your thing

    cont = input("continue?(y/n)")

The code sample would be: 代码示例为:

cont = 'y'

while cont=='y':
    nameone = str(input("Please enter a student name or '*' to finish: "))

    while nameone != "*":
        scoreone = int(input("Please enter a score for " + nameone +": "))

        if scoreone < 0:
            print("positive integers please!")
            break

        else:
            scoretwo = float(input("Please enter another score for "+nameone+": "))
            scorethree = float(input("Please enter another score for "+nameone+": "))

        testscores = scoreone + scoretwo + scorethree    
        avg = testscores / 3    
        print("The average score for",nameone,"is ",avg)

    if nameone == "*":
        print("no bueno") # I have no idea what that means.

    cont = input("continue?(y/n)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM