[英]While loop is not breaking even after the condition is satisfied
我提供了下面的代码。
想要做的是运行第一个输入框,它将检查条件如果为真,它将 go 用于下一个输入(),但如果不是,它将再次运行代码。 问题是第一个 input() 运行良好,但第二个没有退出我正在检查输入是否为 integer 的循环
class AC ():
def __init__(self):
self.owner=input("Enter The Name: ")
while True:
if self.owner.isalpha():
self.balance=(input("Enter The Amount: "))
def lol():
while True:
if self.balance.isdigit():
break
else:
print("Enter The Amount: ")
lol()
break
else:
AC()
break
问题是您的lol()
function 永远不会被调用,因此它将无限期地停留在第一个 while 循环中。
lol()
function 永远不会被调用input()
function 返回字符串值,您可以将 str 更改为 float`
def __init__(self):
self.owner = input("Enter The Name: ")
while True:
if self.owner.isalpha():
self.balance = input("Enter The Amount: ")
def lol():
while True:
try:
self.balance = float(self.balance)
except ValueError:
break
if self.balance.isdigit():
break
else:
lol()
else:
AC()
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.