Python中的无限循环
我不知道怎么了。当我把x*x放在左边,25放在右边时,它不起作用。pythonshell没有显示任何错误,但是在输入解决方案的数量之后,什么也没有发生。我认为它可能处于一个无休止的循环中,或者在每次运行后都不应用x。请帮忙!这是我的密码:Python中的无限循环,python,variables,loops,Python,Variables,Loops,我不知道怎么了。当我把x*x放在左边,25放在右边时,它不起作用。pythonshell没有显示任何错误,但是在输入解决方案的数量之后,什么也没有发生。我认为它可能处于一个无休止的循环中,或者在每次运行后都不应用x。请帮忙!这是我的密码: #getting input information print print "This program cannot solve for irrational or repeating numbers. Please round for them in y
#getting input information
print
print "This program cannot solve for irrational or repeating numbers. Please round for them in your equation."
print
print "Make the variable in your equation stand for x"
print
startingLimit=int(raw_input("What is the lowest estimate that your variable could possibly be?"))
print
wholeNumber=raw_input("Do you know if your variable will be a whole number or a fraction? Answer: yes/no")
if (wholeNumber== "yes"):
print
fraction= raw_input("Is it a decimal/fraction? Answer:yes/no")
if (fraction=="yes"):
print
print "This program will only calculate up to the fourth place to the right of the decimal"
xfinder=0.0001
else:
xfinder=1
else:
xfinder=0.0001
x=0
leftEquation=raw_input("Enter your left side of the equation:")
print
rightEquation=raw_input("Enter the right side of the equation:")
print
amountSolutions=raw_input("How many solutions are there to your equation? (up to 20)")
#solving
indivisualCount=0
count=0
x=startingLimit
while (count!=amountSolutions):
while (count==0):
ifstuffleft=eval(leftEquation)
ifstuffright=eval (rightEquation)
if (ifstuffleft!=ifstuffright):
x=x+xfinder
else:
a=x
count=count+1
while(count==0):
while循环?这将导致它在count
不等于0时(因为它永远不会进入该内部while循环)卡在无限循环中(在while(count!=amountSolutions)中):
循环中)x=x+xfinder
。这意味着您将保持相同的值(在本例中为-5
),直到满足解决方案的数量。因此,无论值是否相等,都必须将该值增加xfinder
a=x
行替换为打印“一个解决方案是”,x
startingLimit = -10
xfinder = 1
leftEquation = "x*x"
rightEquation = "25"
amountSolutions = 2
这a)需要更少的23行代码,使您的问题更容易阅读和理解,b)使测试更容易,这样人们就可以在不回答六个问题的情况下看到问题;c)防止回答者猜测您输入的
启动限制
和数量解决方案
如果为数量解决方案
提供了除1
以外的任何值,则此代码似乎将进入一个错误状态无限循环
while (count!=amountSolutions):
while (count==0):
在上面的解决方案中,一旦找到一个解决方案,
count=1
,那么将跳过内部while循环。程序应该做什么?请求解决方案数量后,任何地方都不会打印任何内容,因此不应该有任何输出。这看起来像是家庭作业。如果是,请按原样标记。@serk:作业标记现在是,不应添加到问题中。通常,使用描述性标题是个好主意。类似于帮助将比python函数中计算值的语法错误引起更少的关注'@David谢谢你让我知道这一点。我不知道。谢谢是的,但我确实告诉它在底部打印解决方案,当我这样写时:if(amountSolutions==“1”):print“solutions=”,a@user1624992:你的问题中没有那个代码。