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

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página