zoukankan      html  css  js  c++  java
  • python基础(六)

    #函数input(),程序暂停运行,等待用户输入一些文本,获取输入后,将其存储在一个变量中
    message = input("tell me")
    print(message)
    tell mehi
    hi
    name = input("Please enter your name:")
    print("Hello, " + name + "!")
    Please enter your name:cui
    Hello, cui!
    prompt = "hihi"
    prompt = "
    what is your name"
    
    name = input(prompt)
    print("
    Hello, " + name + "!")
    what is your namecui
    
    Hello, cui!
    #使用int()来获取数值输入,将用户输入解读为字符串,无法与数值进行比较
    age = input("How old are you?")
    How old are you?22
    age
    '22'
    age >= 18
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-6-4ce6028355cc> in <module>
    ----> 1 age >= 18
    
    TypeError: '>=' not supported between instances of 'str' and 'int'
    age = int(age)
    age >= 18
    True
    height = input("How tall are you, in inches")
    height = int(height)
    How tall are you, in inches71
    if height >= 36:
        print("
    You are tall enough to ride!")
    else:
        print("
    You'll be able to ride")
     
    You are tall enough to ride!
    #求模运算符
    4%3
    #用于判断奇数偶数
    number = input("enter a number, and I'll tell you if it's even or odd:")
    number = int(number)
    
    if number % 2 == 0:
        print("
    The number " + str(number) + "is even.")
    else:
        print("
    The number " + str(number) + "is odd.")
    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program."
    
    message = ""
    while message != 'quit':
        message = input(prompt)
        
        if message != 'quit':
            print(message)
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hihi
    hihi
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.露露
    露露
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.xixi
    xixi
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.quit
    #用于判断奇数偶数
    number = input("enter a number, and I'll tell you if it's even or odd:")
    number = int(number)
    
    if number % 2 == 0:
        print("
    The number " + str(number) + "is even.")
    else:
        print("
    The number " + str(number) + "is odd.")
    enter a number, and I'll tell you if it's even or odd:51
    
    The number 51is odd.
    #while循环
    current_number = 1
    while current_number <= 5:
        print(current_number)
        current_number += 1
    1
    2
    3
    4
    5
    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program."
    message = ""
    while message != 'quit':
        message = input(prompt)
        print(message)
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hi
    hi
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hello
    hello
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.quit
    quit
    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program."
    message = ""
    while message != 'quit':
        message = input(prompt)
        
    if message != 'quit':
        print(message)
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hi
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hello
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.quit
    #使用标志
    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program."
    
    active = True
    while active:
        message = input(prompt)
        
        if message == 'quit':
            active = False
        else:
            print(message)
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hi
    hi
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.hello
    hello
    
    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program.quit
    #使用break退出循环
    prompt = "
    Please enter the name of a city you have visited."
    prompt += "
    (enter 'quit' when you are finished.)"
    
    while True:
        city = input(prompt)
        
        if city == 'quit':
            break
        else:
            print("mmmmmm" + city.title() + "!")
    Please enter the name of a city you have visited.
    (enter 'quit' when you are finished.)nanjing
    mmmmmmNanjing!
    
    Please enter the name of a city you have visited.
    (enter 'quit' when you are finished.)guangdong
    mmmmmmGuangdong!
    
    Please enter the name of a city you have visited.
    (enter 'quit' when you are finished.)quit
    Please enter the name of a city you have visited.
    (enter 'quit' when you are finished.)nanjing
    mmmmmmNanjing!
    
    Please enter the name of a city you have visited.
    (enter 'quit' when you are finished.)guangdong
    mmmmmmGuangdong!
    
    Please enter the name of a city you have visited.
    (enter 'quit' when you are finished.)quit
    #continue
    number = 0
    while number < 10:
        number += 1
        if number % 2 == 0:
            continue
        print(number)
    1
    3
    5
    7
    9
    #避免无限循环
    #处理列表和字典
    unconfirmed_users = ['baba','mama','jing']
    confirmed_users = []
    
    while unconfirmed_users:
        current_user = unconfirmed_users.pop()
        print("vertify:" + current_user.title())
        confirmed_users.append(current_user)
    vertify:Jing
    vertify:Mama
    vertify:Baba
    print("new:")
    for confirmed_user in confirmed_users:
        print(confirmed_user.title())
    new:
    Jing
    Mama
    Baba
    #删除特定值的所有列表元素
    pet = ['dog','cat','dog','goldfish','cat','rabbit','cat']
    print(pet)
    ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
    while 'cat' in pets:
        pets.remove('cat')
        
    print(pets)
    ['dog', 'dog', 'goldfish', 'rabbit']
    #使用用户输入来填充字典
    responses = {}
    
    #设置一个标志,指出调查是否继续
    polling_active = True
    while polling_active:
        #提示输入被调查者的名字和回答
        name = input("
    What is your name?")
        response = input("Which mountain would you like to climb someday?")
        
        #将答卷存储在字典中
        responses[name] = response
        
        #看看是否还有人要参与调查
        repeat = input("Would you like to let another person respond?(yes/no)")
        if repeat == 'no':
            polling_active = False
    What is your name?cui
    Which mountain would you like to climb someday?huang
    Would you like to let another person respond?(yes/no)yes
    
    What is your name?lu
    Which mountain would you like to climb someday?zijin
    Would you like to let another person respond?(yes/no)yes
    
    What is your name?ting
    Which mountain would you like to climb someday?tai
    Would you like to let another person respond?(yes/no)no
     #调查结束,显示结果
    print("
    --- Poll Results ---")
    for name,response in responses.items():
        print(name + " would like to climb " + response + ".")
    --- Poll Results ---
    cui would like to climb huang.
    lu would like to climb zijin.
    ting would like to climb tai.
    
  • 相关阅读:
    oracle删除归档日志
    ORA-16014: log 3 sequence# 540 not archived, no available destinations
    rsync
    vmware 虚拟机导入OVF出现路径错误
    RHEL5.X 重启网卡出现./network-functions: line 78: .: ifcfg-eth0: file not found
    AIX 6.1记录
    CentOS网卡显示为__tmpxxxxxxxx
    PowerPiggy 博客
    利用谷歌浏览器插件获得更好的浏览体验
    C# 的 MSIL 简介
  • 原文地址:https://www.cnblogs.com/Cookie-Jing/p/13627980.html
Copyright © 2011-2022 走看看