一个 While 循环正在覆盖另一个 While 循环
One While Loop Is Overwriting another While Loop
我正在编写一个代码,它将用户输入从字母更改为数字。我用每个字母作为键和相应的数字值制作了一个字典。
为了让代码自运行ning(我不用每次都打运行)我用了一个while循环。
start_over = input("Please Enter (Y) to start and (N) to stop: ").lower()
While start_over == "y" : # First While Loop
(the alphabet changer code here)
start_over = input("Enter (Y) to continue and (N) to stop: ").lower()
While start_over != "y" and start_over != "n" : # Second while loop
start_over = input("Enter (Y) to continue and (N): ")
While start_over == "n": #Third While Loop
print("Thanks for using the code")
break
现在的问题是当我输入 Y 时,第一个 while 循环 运行s 并且它工作正常。如果我输入 N,第三个 while 循环 运行s 并且代码会按需要中断。但是当我把除了 Y 和 N 之外的任何东西放在第二个 while 循环 运行s 并要求我放任何其他东西时。当我将 N 放入第二个 while 循环时,代码会按需中断。但是当我在第二个 while 循环中输入 Y 时,代码中断而不是 运行ning 第一个 while 循环。为什么会这样我不知道。
问题是当您的代码进入第二个 while
循环并且用户输入 start_over = 'y'
时。它不会移动到您的第一个 while
循环,因为它位于上方。为避免这种情况,您可以将第二个 while
放在一个函数中:
def start(start_over):
while start_over != "y" and start_over != "n" :
start_over = input("Enter (Y) to continue and (N): ")
return start_over
start_over = input("Please Enter (Y) to start and (N) to stop: ").lower()
if start_over != "y" and start_over != "n" :
start_over = start(start_over)
while start_over == "y" :
start_over = input("Enter (Y) to continue and (N) to stop: ").lower()
if start_over != "y" and start_over != "n" :
start_over = start(start_over)
while start_over == "n": #Third While Loop
print("Thanks for using the code")
break
我正在编写一个代码,它将用户输入从字母更改为数字。我用每个字母作为键和相应的数字值制作了一个字典。
为了让代码自运行ning(我不用每次都打运行)我用了一个while循环。
start_over = input("Please Enter (Y) to start and (N) to stop: ").lower()
While start_over == "y" : # First While Loop
(the alphabet changer code here)
start_over = input("Enter (Y) to continue and (N) to stop: ").lower()
While start_over != "y" and start_over != "n" : # Second while loop
start_over = input("Enter (Y) to continue and (N): ")
While start_over == "n": #Third While Loop
print("Thanks for using the code")
break
现在的问题是当我输入 Y 时,第一个 while 循环 运行s 并且它工作正常。如果我输入 N,第三个 while 循环 运行s 并且代码会按需要中断。但是当我把除了 Y 和 N 之外的任何东西放在第二个 while 循环 运行s 并要求我放任何其他东西时。当我将 N 放入第二个 while 循环时,代码会按需中断。但是当我在第二个 while 循环中输入 Y 时,代码中断而不是 运行ning 第一个 while 循环。为什么会这样我不知道。
问题是当您的代码进入第二个 while
循环并且用户输入 start_over = 'y'
时。它不会移动到您的第一个 while
循环,因为它位于上方。为避免这种情况,您可以将第二个 while
放在一个函数中:
def start(start_over):
while start_over != "y" and start_over != "n" :
start_over = input("Enter (Y) to continue and (N): ")
return start_over
start_over = input("Please Enter (Y) to start and (N) to stop: ").lower()
if start_over != "y" and start_over != "n" :
start_over = start(start_over)
while start_over == "y" :
start_over = input("Enter (Y) to continue and (N) to stop: ").lower()
if start_over != "y" and start_over != "n" :
start_over = start(start_over)
while start_over == "n": #Third While Loop
print("Thanks for using the code")
break