你好,我在制作这个循环时遇到了麻烦,而不是在我宣布" 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")
def main():
GetPosInt()
答案 0 :(得分:1)
&#34; ...它会在分配前引用错误&#34;#34; [...]
这是因为,您可能会指定条件为while nameone != '*':
,但在您进入循环之前不会声明nameone
。你可以轻松解决这个问题。试试这个:
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")
答案 1 :(得分:0)
如果我对你的问题的理解是正确的,那么你需要的是一个外循环。
类似的东西:
jenkins.sh
代码示例如下:
cont = 'y'
while cont=='y':
# Do your thing
cont = input("continue?(y/n)")