美文网首页
6.python的input函数与while循环

6.python的input函数与while循环

作者: 陶不渊 | 来源:发表于2018-09-30 17:11 被阅读0次

    1. input()函数

    input()函数可以在括号中写入提示,用户根据提示输入信息,并且将信息作为一个字符串,保存在变量中。

    输入:

    prompt='If you tell us who you are, we can personalize the messages you see.'
    prompt+='\nWhat is your first name?  '  #prompt第二种方法
    
    first_name=input(prompt)
    last_name=input('What is your last name?  ')    #prompt第一种方法
    
    print('\nhello, '+first_name+' '+last_name+'!')
    

    输出:

    c:\py>input
    If you tell us who you are, we can personalize the messages you see.
    What is your first name?  tao
    What is your last name?  ming
    
    hello, tao ming!
    

    2. int()函数

    如果你要用户输入数字,并且要将其作为整型变量用来进行数字方面的处理,需要使用int()函数。

    输入:

    height=input('how old are you?')
    height=int(height)
    
    if height>=18:
        print('you are old enogh')
    else:
        print('you are too young')
    

    输出:

    c:\py>int_input
    how old are you?16
    you are too young
    
    c:\py>int_input
    how old are you?19
    you are old enogh
    

    3. while循环

    当while后面的值为true时,执行while下面缩进的语句。

    输入:

    prompt='\nTell me something,and I will repeat it back to you:'
    prompt+="\nenter 'quit' to end the program.\n"
    
    message=""  #一开始为空字符串,但它依然为字符串,这样才能比较
    while message != 'quit':
        message = input(prompt)
        
        if message != 'quit':
            print(message)
    

    输出:

    c:\py>while
    
    Tell me something,and I will repeat it back to you:
    enter 'quit' to end the program.
    we
    we
    
    Tell me something,and I will repeat it back to you:
    enter 'quit' to end the program.
    quit
    

    4. 用Flag控制while循环

    while Falg:那我们可以通过控制Flag的True或者Flase来控制程序流。

    输入:

    prompt='\nTell me something,and I will repeat it back to you:'
    prompt+="\nenter 'quit' to end the program.\n"
    
    active = True
    
    while active:
        message = input(prompt)  
        if message == 'quit':
            active = False
        else:
            print(message)
    

    输出:

    c:\py>flag
    
    Tell me something,and I will repeat it back to you:
    enter 'quit' to end the program.
    else
    else
    
    Tell me something,and I will repeat it back to you:
    enter 'quit' to end the program.
    quit
    
    c:\py>
    

    5. break语句

    若在循环中遇到break语句,程序跳过剩余的循环语句,直接跳出循环。

    输入:

    prompt='\nTell me something,and I will repeat it back to you:'
    prompt+="\nenter 'quit' to end the program.\n"
    
    #和上一个程序比,因为有了break语句,判别是否跳出循环的任务就交给了if
    while True:     
        message = input(prompt)  
        if message == 'quit':
            break
        else:
            print(message)
    

    输出:

    Tell me something,and I will repeat it back to you:
    enter 'quit' to end the program.
    me
    me
    
    Tell me something,and I will repeat it back to you:
    enter 'quit' to end the program.
    quit
    
    c:\py>
    

    6. continue语句

    在循环中,continue语句会跳过剩余的循环语句,回到循环的开头,重新开始循环。即跳过本次剩余的循环,直接开始下一个循环。

    输入:

    current_number=0
    while current_number <= 10:
        current_number+=1
        if current_number%2==0:
            continue
        print(current_number)
    

    输出:

    c:\py>continue_
    1
    3
    5
    7
    9
    11
    

    程序中,while循环的条件是current_number<=10,但当current_number=10时,符合条件,while又进行一次循环,使current_number+1后变成11输出,在检测发现11不再<=10,所以跳出循环。

    7. 用while循环处理列表

    (1) 将一个列表元素移到另一个列表

    输入:

    unconfirmed_users=['alice','kitty','john']
    confirmed_users=[]
    
    while unconfirmed_users:
        current_user=unconfirmed_users.pop()
        
        print("verifying user: "+current_user.title())
        confirmed_users.append(current_user)
    
    print('\nthe following users are confirmed: ')
    for confirmed_user in confirmed_users:
        print(confirmed_user.title())   #方法一定要带()
    

    输出:

    c:\py>while_pop_append
    verifying user: John
    verifying user: Kitty
    verifying user: Alice
    
    the following users are confirmed:
    John
    Kitty
    Alice
    

    (2) 用while循环和.renmove()方法移除所有指定元素

    .remove()方法只能移除移除第一个指定的元素,加上while循环,可以移除所有的指定元素。

    输入:

    pets=['dog','cat','goldfish','cat','rabbit','cat']
    print(pets)
    
    while 'cat' in pets:
        pets.remove('cat')
        
    print(pets)
    

    输出:

    c:\py>while_remove
    ['dog', 'cat', 'goldfish', 'cat', 'rabbit', 'cat']
    ['dog', 'goldfish', 'rabbit']
    

    (3) 用while循环填充字典

    输入:

    prompt_1='\nwho are you?'
    prompt_2='what food do you like best?'
    
    responses={}
    
    while True:
        name=input(prompt_1)
        food=input(prompt_2)
        
        responses[name]=food    #添加键值对只需一条语句即可
        answer=input('would you like to let another person respond?(yes/no)')
        if answer=='no':
            break
    print('\n this is poll results\n')
    print(responses)
    

    输出:

    c:\py>while_dic
    
    who are you?mike
    what food do you like best?pear
    would you like to let another person respond?(yes/no)yes
    
    who are you?jack
    what food do you like best?apple
    would you like to let another person respond?(yes/no)yes
    
    who are you?john
    what food do you like best?strawberry
    would you like to let another person respond?(yes/no)no
    
     this is poll results
    
    {'mike': 'pear', 'jack': 'apple', 'john': 'strawberry'}
    

    相关文章

      网友评论

          本文标题:6.python的input函数与while循环

          本文链接:https://www.haomeiwen.com/subject/okgwoftx.html