#๐Ÿ”’ Program stopping prematurely

39 messages ยท Page 1 of 1 (latest)

tacit geyser
#

instructions: Please write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. See the examples below for details:

wet girderBOT
#

@tacit geyser

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tacit geyser
#
num = int(input("Please type in a number:"))
a = 1
b = 1
while a != num and b != num:
    print(f"{a} x {b} = {a*b}")
    while b != num:
        b += 1
        print(f"{a} x {b} = {a*b}")
    b = 1
    a += 1```
#

im not sure why the program is stopping already

#

the while conditions havent been met yet

#

a is == num but b isnt yet

#

isnt the program supposed to keep running til both are == num

#

Please type in a number:4
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12

timber iris
tacit geyser
timber iris
timber iris
digital wing
#

I would use for loops and range to accomplish this instead. Would make the program a lot simpler and easier to understand. And it would fix the issue you're having as well.

tacit geyser
#

those concepts havent been introduced in this part of the course yet so I was hoping to do it with a while loop

timber iris
#

maybe while a < num ... ?

tacit geyser
#

theres a lot of problems in this course that would be a lot easier with range and stuff ๐Ÿ˜ญ but I figured it felt a bit like cheating

tacit geyser
timber iris
#

They challenge you to do that on purpose

tacit geyser
#

Yeah I figured

tacit geyser
timber iris
digital wing
#

Changing the outer while to while not a > num: worked for me

tacit geyser
#

they worked tysm guys!

#

I need help in understanding why my code didnt work though

#

I still dont get why the program seemed to stop before the conditions were met

timber iris
#

well think about it

#

if while a < num means while 4 < 4 it would stop before 4

#

not on 4

#

or in PeachTeaEnjoyer's case
while not a > num is equivalently read as while not 4 greater than 4

#

which is True

#

until it goes while not 5 greater than 4, thats False and stops there

tacit geyser
#

I see

#

thank you so much!

#

!close

wet girderBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.