0

I can't get it to actually repeat the while loop. I've tried having it register a true value, or make it continue or break out of the loop. But nothing works.

xvalue = int(input("Enter a test value to test if it works: "))

while xvalue >= Limit:
    print("\a\a\a")
else:
    continue
    xvalue = int(input("Updating Value: "))

Can someone suggest something?

I've also written it so that it says:

else:
     return True 

But that doesn't work. (I get an error)I just need it to keep repeating the while loop until it becomes true on the first condition. And then rings.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Micrified
  • 3,338
  • 4
  • 33
  • 59

3 Answers3

3

I don't entirely follow the intent of your code. I think you want something along the following lines:

while True:
   xvalue = int(input("Enter a value: "))
   if xvalue < Limit:
      break
   print("\a\a\a")
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Sorry, I realized it's unclear. The intent here is to have it ring when the xvalue surpasses limit. I just can't get it to actually continue to ask for the value when it doesn't surpass so that it repeats itself and therefore 'checks' itself. – Micrified Dec 06 '12 at 18:35
  • This unfortunately has the same problem my other attempts encountered. – Micrified Dec 06 '12 at 18:43
  • @user1883304: That's what my answer does. You may need to change `<` to `<=`, depending on whether `Limit` should be included in the range. – NPE Dec 06 '12 at 18:43
  • It does the same thing my last attempts did. After asking for the updated value once, it actually just stops there, and doesn't ask again. Not sure why. – Micrified Dec 06 '12 at 18:44
  • @user1883304: Did you preserve the indentation when testing it? – NPE Dec 06 '12 at 18:44
  • Yes, I did. Same problem. :( – Micrified Dec 06 '12 at 18:50
1

There are a bunch of problems with your code, but there are a few big problems here.

First, the else in while...else doesn't mean what you think it does. It's not like in if...else. In while...else, the else block is executed if your while statement becomes False--note that that does not include if you break out of the loop or there's an error. In your code, the else block would be executed when xvalue < Limit, since that's the opposite of your Boolean expression for the while.

Second, because the else block is executed after the loop, putting continue in there doesn't make any sense, since there's no longer any loop to iterate over. Not only that, even if there were a loop continuing, the fact that you stuck continue before xvalue = int(input... means that the loop will be restarted before the user gets a chance to put in an updated value. You would need to put continue after the reassignment, and at that point, there's no point to putting in the continue at all.

So basically, what you're looking for is:

xvalue = int(input("Enter a test value to see if it works: "))

while xvalue >= Limit:
    print ("\a\a\a")
    xvalue = int(input("Please try another value: "))

Updated after OP comments:

xvalue = int(input("Enter a test value to see if it works: "))

while xvalue < Limit:                                     # Repeats until user gives a value above limit
    xvalue = int(input("Please try another value: "))
else:
    while True:                                           # Causes bell to ring infinitely
        print ("\a\a\a")
Community
  • 1
  • 1
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
  • Well, the thing is. I want the loop to work so that if the xvalue exceeds limit, it rings the little bell alarm. And if it doesn't, then it keeps asking for you to update the value. It appears to me like if the limit is exceeded, it only rings once, and then asks for another value. I want it to ring continuously if broken, or ask for values until it is. – Micrified Dec 06 '12 at 18:52
  • @user Well, you definitely didn't make that clear in your question. Updated. – jdotjdot Dec 06 '12 at 18:57
  • Sorry.Thank you for updating. I didn't know you could put while true in an else. :D – Micrified Dec 06 '12 at 19:01
  • @user1883304 Glad to hear that. If this is the answer that worked for, it would be great if you would accept it (by clicking the check mark) so that future readers will know that this was the best answer for you. – jdotjdot Dec 07 '12 at 03:08
0

Can someone suggest something I've written it so that it says: else: return True but that doesn't work. I just need it to keep repeating the while loop until it becomes true on the first condition. And then rings.

You could do the following:

Value = False

while Value == False:
    #code to do while looping

#code to do after looping

or:

Value = False

While 1:
   #code for inside the loop
   if Value == True
       break

Furthermore, your question was not really describing enough, so don't blame me if i didn't understand

MichaelvdNet
  • 1,074
  • 2
  • 14
  • 22