while 循环没有给出预期的结果
while loop is not giving expected results
我正在学习python3.6
在编写脚本时我遇到了一个问题:
下面是我的代码
from sys import exit
print("Welcome to the official game designed by Prince Bhatia")
print("Copywrite @princebhatia")
def list1():
loop = 5
while loop == 5:
print("Game starts here")
list1 = ["Rahul", "Prince", "Sam", "Sonu"]
print("which Player do you choose?")
print("Now the game starts")
name1 = input()
if "Rahul" in name1 or "rahul" in name1:
print("Rahul Enters the room1")
elif "Prince" in name1 or "prince" in name1:
print("Prince Enters the room2")
elif "Sam" in name1 or "sam" in name1:
print("Sam Enters the room3")
elif "Sonu" in name1 or "sonu" in name1:
print("Sonu Enters the room4")
else:
print("Please enter the valid name")
loop = 6
list1()
print("""-------------------------------------""")
但问题是,每当我输入定义的名称时,它都能正常工作,但当我输入错误的名称时,它会停止,但我想要的应该让我一直询问 "please enter valid name ",直到我输入有效名称。
任何建议。 python3.6
这里给space打算给输入码
您可以在 else
中将 5 赋值给 loop
,并将 6 的赋值向上移动到同一变量:
from sys import exit
print("Welcome to the official game designed by Prince Bhatia")
print("Copywrite @princebhatia")
def list1():
loop = 5
while loop == 5:
print("Game starts here")
list1 = ["Rahul", "Prince", "Sam", "Sonu"]
print("which Player do you choose?")
print("Now the game starts")
name1 = input()
loop = 6
if "Rahul" in name1 or "rahul" in name1:
print("Rahul Enters the room1")
elif "Prince" in name1 or "prince" in name1:
print("Prince Enters the room2")
elif "Sam" in name1 or "sam" in name1:
print("Sam Enters the room3")
elif "Sonu" in name1 or "sonu" in name1:
print("Sonu Enters the room4")
else:
print("Please enter the valid name")
loop = 5
list1()
print("""-------------------------------------""")
无论如何,改进这个变量的名称和值是个好主意。最好在此处使用名称为 isInvalidName 的布尔变量。所以你 while
会是:while isInvalidName:
首先,你的缩进是错误的。
问题:不管你是否得到匹配的答案,你都在设置 loop = 6。
这里有一个稍微不同的建议:
print("Game starts here")
print("which Player do you choose?")
print("Now the game starts")
while True:
name1 = input()
if "Rahul" in name1 or "rahul" in name1:
print("Rahul Enters the room1")
break
elif "Prince" in name1 or "prince" in name1:
print("Prince Enters the room2")
break
elif "Sam" in name1 or "sam" in name1:
print("Sam Enters the room3")
break
elif "Sonu" in name1 or "sonu" in name1:
print("Sonu Enters the room4")
break
else:
print("Please enter the valid name")
这样我们在获得有效输入后就中断循环,否则我们将无限期地循环。
我正在学习python3.6 在编写脚本时我遇到了一个问题:
下面是我的代码
from sys import exit
print("Welcome to the official game designed by Prince Bhatia")
print("Copywrite @princebhatia")
def list1():
loop = 5
while loop == 5:
print("Game starts here")
list1 = ["Rahul", "Prince", "Sam", "Sonu"]
print("which Player do you choose?")
print("Now the game starts")
name1 = input()
if "Rahul" in name1 or "rahul" in name1:
print("Rahul Enters the room1")
elif "Prince" in name1 or "prince" in name1:
print("Prince Enters the room2")
elif "Sam" in name1 or "sam" in name1:
print("Sam Enters the room3")
elif "Sonu" in name1 or "sonu" in name1:
print("Sonu Enters the room4")
else:
print("Please enter the valid name")
loop = 6
list1()
print("""-------------------------------------""")
但问题是,每当我输入定义的名称时,它都能正常工作,但当我输入错误的名称时,它会停止,但我想要的应该让我一直询问 "please enter valid name ",直到我输入有效名称。 任何建议。 python3.6
这里给space打算给输入码
您可以在 else
中将 5 赋值给 loop
,并将 6 的赋值向上移动到同一变量:
from sys import exit
print("Welcome to the official game designed by Prince Bhatia")
print("Copywrite @princebhatia")
def list1():
loop = 5
while loop == 5:
print("Game starts here")
list1 = ["Rahul", "Prince", "Sam", "Sonu"]
print("which Player do you choose?")
print("Now the game starts")
name1 = input()
loop = 6
if "Rahul" in name1 or "rahul" in name1:
print("Rahul Enters the room1")
elif "Prince" in name1 or "prince" in name1:
print("Prince Enters the room2")
elif "Sam" in name1 or "sam" in name1:
print("Sam Enters the room3")
elif "Sonu" in name1 or "sonu" in name1:
print("Sonu Enters the room4")
else:
print("Please enter the valid name")
loop = 5
list1()
print("""-------------------------------------""")
无论如何,改进这个变量的名称和值是个好主意。最好在此处使用名称为 isInvalidName 的布尔变量。所以你 while
会是:while isInvalidName:
首先,你的缩进是错误的。
问题:不管你是否得到匹配的答案,你都在设置 loop = 6。
这里有一个稍微不同的建议:
print("Game starts here")
print("which Player do you choose?")
print("Now the game starts")
while True:
name1 = input()
if "Rahul" in name1 or "rahul" in name1:
print("Rahul Enters the room1")
break
elif "Prince" in name1 or "prince" in name1:
print("Prince Enters the room2")
break
elif "Sam" in name1 or "sam" in name1:
print("Sam Enters the room3")
break
elif "Sonu" in name1 or "sonu" in name1:
print("Sonu Enters the room4")
break
else:
print("Please enter the valid name")
这样我们在获得有效输入后就中断循环,否则我们将无限期地循环。