Python 3. X basic tutorial 3

Posted by opencombatclan on Wed, 13 May 2020 17:00:53 +0200

Hello, I'm a little circle who loves to learn programming. Today I'll talk about the if/elif/else statement

code

import time as t
a = 0
while a < 1:
    x = input('Please enter a number (1-9)\n')
    y = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    if x in y:
        print('The input is correct!')
        t.sleep(5)
        a=a + 1
        pass
    else:
        print('Input error')
        pass
Code instance

Click Openhave a look

Most of the content is relatively simple, and I'll make do with it

First of all, let's talk about the most important part of our class (you can watch it more often if you haven't seen it yet)

if...:

...

pass

else:

...

pass

Of course, if...:

...

pass

elif ... :

...

pass

else:

...

pass

Of course, it's OK to pass without adding, but it's easier for people or interpreters to understand

Of course, it's easier. That's the list

For example, if you use the if statement to detect many things, you don't know the list, so you just learn if

Its syntax is

# list_name = [...]
# For example, the list name is path,The content is ko,pass,bi,You can write like this
path = ['ko', 'pass', 'bi']

It is used to check whether you input 1-3 numbers in the input box. It can be written as follows

x = input('Please enter 1-3 The number of')
 number_no = ['4', '5', '6', '7', '8', '9']
if x in number_no :
    print('Detection failed')
    pass
else:
    print('Test successful')
    pass
# Note: number_no Is the list name

or

x = input('Please enter 1-3 The number of')
number = ['1', '2', '3']
if x in number:
    print('Test successful')
    pass
else:
    print('Detection failed')
    pass
# Note: number Is the list name

I don't need to shiver

I'm going to do my homework. I'll come back after finishing my homework. I'll give you my lesson preparation code, but don't be lazy and copy it directly. If you don't, your code writing speed will be improved

 

import time as t
a = 0
while a < 1:
    x = input('Please enter a number (1-9)\n')
    y = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    if x in y:
        print('The input is correct!')
        t.sleep(5)
        a=a + 1
        pass
    else:
        print('Input error')
        pass
# Don't forget to install Python Interpreter(^U^)ノ~

Annotated version

# Import time Module alias t
import time as t

# definition a The default value of the variable is 0
a = 0

# whlie Cycle arrival condition( a If the variable is less than 1), it will cycle all the time
while a < 1:
    # variable x Storage input Box contents
    x = input('Please enter a number (1-9)\n')
    # list y Storage 1, 2, 3, 4, 5, 6, 7, 8, 9
    y = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    """ 
    if/else Statement block
    """
    # if Detect variables x Is the content in y If there is any in the list, execute the following statement block
    if x in y:
        # Display input correct
        print('The input is correct!')
        # Pause for five seconds
        t.sleep(5)
        # a=a + 1 Equal to a =+ 1 variable a+1
        a = a + 1
        # It doesn't work
        pass
    # If not, execute the following statement block
    else:
        # Display input error
        print('Input error')
        # It doesn't work
        pass
    # It doesn't work
    pass
""" 
//Summary: 1.import  Time import import module time as alias t
    2.a = 0 definition a The default value of the variable is 0
    3.while a < 1 whlie Cycle arrival condition( a If the variable is less than 1), it will cycle all the time
    4.x = input('Please enter a number (1-9)\n') variable x Storage input Box contents
    5.y = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] list y Storage 1, 2, 3, 4, 5, 6, 7, 8, 9
    6.if x in y: if Detect variables x Is the content in y Execute if any in the list if Statement block for
    7.print('The input is correct!') Display input correct
    8.a = a + 1 a=a + 1 Equal to a =+ 1 variable a+1
    9.pass It doesn't work
    10.else: If not else Statement block for
    11.print('Input error') Display input error
    end
"""

Topics: Python less Programming