0

I am new with python and coding and I am trying to learn how to use for and while functions. I am trying to create program that asks from user two values (valueA and valueB). and in each loop valueA doubles and valueB grows by a hundred. And the loop should stop if valueA is greater than valueB. or valueB or valueA is greater than 10000.

a = int(input("Give value a: "))
b = int(input("Give value b: "))

while (True):
    print(a, b)
    a *= 2
    b += 100
    if a > b:
        break
    if a or b > 10000:
        break

This does not work..

image

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
moukkuz
  • 3
  • 1
  • 2
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – UnholySheep Sep 24 '20 at 14:45
  • 1
    You can replace `if a or b > 10000:` with `if a > 10000 or b > 10000:` – Stef Sep 24 '20 at 14:45
  • But actually it would be much better to use those conditions directly as the while condition, instead of writing `while True` followed by `break` statements: `while (a <= b and a <= 10000 and b <= 10000): a *= 2; b+= 100` – Stef Sep 24 '20 at 14:46

1 Answers1

0

As already mentioned in the comments to your questions, the issue is your second if statement. Try changing it to

if a > 10000 or b > 10000:
    break

However, the cleaner solution would be to include the conditions in the if statements already in the while loop condition. This can look something like

a *= 2
b += 100 
while (a <= b) and (a <= 10000 or b <= 10000):
    print(a, b)
    a *= 2
    b += 100  
Stefan
  • 1,697
  • 15
  • 31
  • @OneCricketeer You are of course right... I updated my answer – Stefan Sep 24 '20 at 15:11
  • That almost work the way I want. It still miss one line of numbers. Like if I use a: 2 and b: 2. Then the last row should be a: 1024 and b: 902. Hope you understand. – moukkuz Sep 24 '20 at 15:21
  • @moukkuz Why not just print the final values again at the end? – Stefan Sep 24 '20 at 15:26