Sam Brookefeld
Sam Brookefeld

Reputation: 1

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

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.

Upvotes: -1

Views: 70

Answers (1)

ticktalk
ticktalk

Reputation: 922

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'>

Upvotes: 0

Related Questions