2012-10-17 358 views
0

我不知道什么是错的。当我在左侧放置x * x,在右侧放置25时,它不起作用。 python shell显示没有错误,但在输入解决方案的数量后,没有任何反应。我认为它可能处于无限循环中,或者在每次运行后都不会应用x。请帮忙!这里是我的代码:Python中无限循环while循环

#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 
+3

该程序应该做什么?在请求解决方案的数量后,任何地方都不会打印任何内容,所以不应该有任何输出。 –

+0

这看起来像功课。如果是这样,请标记它。 – serk

+0

@serk:家庭作业标签现在[正式弃用](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated),不应添加到问题 –

回答

2
  1. 为什么你有内部while (count==0): while循环?这会导致它在count不等于0(因为它永远不会进入内部while循环)时陷入无限循环(在while (count!=amountSolutions):循环中)。

  2. 修复该问题后,请注意,如果值相互相等,则不执行x=x+xfinder。这意味着您将保持相同的价值(在这种情况下为-5),直到您满足解决方案的数量。无论这些值是否相等,您都必须增加xfinder的值。

  3. 您从不打印解决方案或对其执行任何操作。你可能想用print "One solution is", x

最后,更换a=x线,当你发布你应该争取最小例如一个问题。所有的输入代码可以通过硬编码的5个变量来代替,一样的东西:

startingLimit = -10 
xfinder = 1 
leftEquation = "x*x" 
rightEquation = "25" 
amountSolutions = 2 

这一个)需要的代码23行较少,使您的问题更容易阅读和理解,B),使得它更容易测试所以人们可以在不回答六个问题的情况下看到问题,并且c)防止回答者不必猜测您为startingLimitamountSolutions输入的内容。

+0

是的,但我确实告诉它打印出底部的解决方案时,我把这个:if(amountSolutions ==“1”): print“Solutions =”,a – user1624992

+0

@ user1624992:该代码是不存在你的问题。 –

0

如果除1以外的任何值给出amountSolutions,它似乎会进入无限循环。

while (count!=amountSolutions): 
    while (count==0): 

在上述一次一个解决方案被发现,count = 1并且因此内部而循环将被跳过。