Python: Using while loop and user input to run a program until prompted to terminate - Stack Overflow

admin2025-04-17  6

I am having trouble with an early coding assignment. Nothing relating to this purpose except for the very basics of what a while loop is has been taught.

def branching_looping():
    while True:
        x=[1,2,3,4,5,6,7,8,9,10]
        for i in x:
            print(i)

        y=1
        for y in range(10):
            print(f"{y+1}")
#Array can be automatically generated by giving its length. To avoid an output of 0, add one to starting value.

        n = int(input("Please define n:"))
        count = n
        total = 0

        while count:
            total += count
            count -= 1

        print(f"The sum of the range of {n}  is {total}")

#Repeatedly adds the current value, subtracts one from the current value, then adds the resulting value again.

        z=int(input("Please input number:"))
        if z //2 is int:
            z=False
        else:
            z=True
            print("This is an odd number!")

        terminate=input("Would you like to exit the program? y/n:")
        if terminate=="y":
            print("Goodbye!")
            break
        else:
            print("From the top, then!")

When the last input prompt and related if-else statement is added, everything breaks. The terminal does not give me any of the input prompts and simply displays whatever is entered.

I have tried defining and calling functions before and inside of the loop, to no avail.

I am aware that there are other problems with what I have here. I'm specifically looking for answers about how to terminate the loop with user input.

I am having trouble with an early coding assignment. Nothing relating to this purpose except for the very basics of what a while loop is has been taught.

def branching_looping():
    while True:
        x=[1,2,3,4,5,6,7,8,9,10]
        for i in x:
            print(i)

        y=1
        for y in range(10):
            print(f"{y+1}")
#Array can be automatically generated by giving its length. To avoid an output of 0, add one to starting value.

        n = int(input("Please define n:"))
        count = n
        total = 0

        while count:
            total += count
            count -= 1

        print(f"The sum of the range of {n}  is {total}")

#Repeatedly adds the current value, subtracts one from the current value, then adds the resulting value again.

        z=int(input("Please input number:"))
        if z //2 is int:
            z=False
        else:
            z=True
            print("This is an odd number!")

        terminate=input("Would you like to exit the program? y/n:")
        if terminate=="y":
            print("Goodbye!")
            break
        else:
            print("From the top, then!")

When the last input prompt and related if-else statement is added, everything breaks. The terminal does not give me any of the input prompts and simply displays whatever is entered.

I have tried defining and calling functions before and inside of the loop, to no avail.

I am aware that there are other problems with what I have here. I'm specifically looking for answers about how to terminate the loop with user input.

Share edited Jan 31 at 1:26 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Jan 31 at 1:17 Sam BrookefeldSam Brookefeld 11 bronze badge 11
  • 1 Replicating your code comes up with no errors for me and I can run the series of inputs without error. Are you sure your indentation is correct? – carly m Commented Jan 31 at 1:25
  • if z //2 is int: is not how you tell if a number if an integer. This condition will always be false. Also, z//2 is always an integer. – Barmar Commented Jan 31 at 1:27
  • 1 The way to tell if a number is odd or even is to test if z % 2 is 1 or 0. – Barmar Commented Jan 31 at 1:28
  • @carlym If I change any indents it throws errors. I'll keep poking at it. – Sam Brookefeld Commented Jan 31 at 2:18
  • @SamBrookefeld Unfortunately it is hard to say what the problem is given I can run your code perfectly fine. The issue could be related to your IDE. When you say "everything breaks", what exactly are the errors you're getting? – carly m Commented Jan 31 at 2:22
 |  Show 6 more comments

1 Answer 1

Reset to default 0

assume nothing ....

#
# as floats
#
x=15.0
y=2.0
print(f'x:{x}, y:{y} , x//y==:{x//y} x%y=={x%y} type(x//y):{type(x//y)}, type(x%y):{type(x%2)}')
x:15.0, y:2.0 , x//y==:7.0 x%y==1.0 type(x//y):<class 'float'>, type(x%y):<class 'float'>

#
# as integers
#
x=15
y=2
print(f'x:{x}, y:{y} , x//y==:{x//y} x%y=={x%y} type(x//y):{type(x//y)}, type(x%y):{type(x%2)}')
x:15, y:2 , x//y==:7 x%y==1 type(x//y):<class 'int'>, type(x%y):<class 'int'>
转载请注明原文地址:http://www.anycun.com/QandA/1744884105a88985.html