Home > OS >  I have troubles making a while loop in python
I have troubles making a while loop in python

Time:11-25

I have been meaning to make a program where u can add items to it until you type a certain command so it stopes this is my code( an absolute beginner in coding and in English)

CodePudding user response:

You could use a set of strings that contain the possible inputs that can stop the program:

QUIT_INPUTS = {'quit', 'q'}


def main() -> None:
    print('Deadly Carrot\'s Python Item Input Program')
    print('=========================================')
    items = []
    user_input = input('Enter an item or quit: ')
    while user_input.lower() not in QUIT_INPUTS:
        items.append(user_input)
        user_input = input('Enter an item or quit: ')
    if not items:
        print('You entered 0 items...')
    else:
        print(f'You entered {len(items)} items:')
        print('\n'.join(items))
    print('Goodbye!')


if __name__ == '__main__':
    main()

Example Usage 1:

Deadly Carrot's Python Item Input Program
=========================================
Enter an item or quit: Apple
Enter an item or quit: Banana
Enter an item or quit: Carrot
Enter an item or quit: Quit
You entered 3 items:
Apple
Banana
Carrot
Goodbye!

Example Usage 2:

Deadly Carrot's Python Item Input Program
=========================================
Enter an item or quit: q
You entered 0 items...
Goodbye!

CodePudding user response:

I can't see your code.

go = ""
while go != "stop":
    go = input("Type:")
    print(go)

Something like this?

  • Related