Constantly looking for user input in Python

前端 未结 2 638
醉酒成梦
醉酒成梦 2021-02-11 03:36

How would I write a Python program that would always be looking for user input. I think I would want to have a variable equal to the input and then something different would hap

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-11 04:40

    from http://www.swaroopch.com/notes/Python_en:Control_Flow

    #!/usr/bin/python
    # Filename: while.py
    
    number = 23
    running = True
    
    while running:
        guess = int(input('Enter an integer : '))
    
        if guess == number:
            print('Congratulations, you guessed it.')
            running = False # this causes the while loop to stop
        elif guess < number:
            print('No, it is a little higher than that.')
        else:
            print('No, it is a little lower than that.')
    else:
        print('The while loop is over.')
        # Do anything else you want to do here
    
    print('Done')
    

提交回复
热议问题