• if else


    PYTHON之路

    七、基本的if判断

    最简单的流程处理: if ...else

    If简单练习:

    #!/usr/bin/env  python

    # -*-coding:utf-8 -*-

    #if 基本表达式

    #用户登录验证

    #提示输入用户名和密码

    #验证用户名和密码

    #如果错误,则输出用户名或密码错误;

    #如果成功,则输出欢迎,XXX

     

    user = 'zhang'

    password = '123.com'

    username = input("username:")

    password = input("password:")

    if user == username:

    print("用户名输入正确")   {下层代码}

     

     

    下层代码:

    这个代码的运行要依赖于上一层代码,也就是说上一层代码必须为真(成立),才会读下层代码。///可以理解为上一层的子代码

    子代码不能顶行写,否则Python认为是不相关的代码,将会独立执行

    Python的默认开发规范:子代码空4

    If ...else语句练习:

    #!/usr/bin/env  python

    # -*-coding:utf-8 -*-

    user = 'zhangjianghua'

    passwd = '123.com'

    username = input("username:")

    password = input("password:")

    if user == username :

        print("username is correct...")

        if password == passwd:

            print("welcome login...")

        else:

            print("password is invalid...")

    else:

        print("连用户名都没蒙对,滚粗。。。")

    If...else V2 优化版:

    #!/usr/bin/env  python

    #-*-coding:utf-8 -*-

    user = "zhangjianghua"

    passwd = "123.com"

    username = input("username:")

    password = input("password:")

    if user == username and passwd == password:

        print("welcome login")

    else:

        print("Invalid username or password....")

    八、猜年龄游戏

    (猜年龄游戏V1,猜对即退出)

    #!/usr/bin/env  python

    #-*-coding:utf-8 -*-

    age = 19

    guess_num = int (input("input your guess num:"))

    if guess_num ==age:

        print("Congratulations! you got it.")

    elif guess_num >age:

        print("Think smaller!")

    else:

        print("Think Big...")

    九、for循环

    for i in range(10):

    print(i)

    i  每循环一次i 就会增加1

    (猜年龄游戏V2,最大次数三次)

    #!/usr/bin/env  python

    #-*-coding:utf-8 -*-

    age = 19

    for i in range(10):

        if i <3:

            guess_num = int(input("input your guess num:"))

            if guess_num ==age :

                print("恭喜你猜对了")

                break #猜对即停止,不往后走了,跳出整个loop

            elif guess_num >age:

                print("往小了想!")

            else:

                print("往大了想!")

        else:

            print("尝试了太多次..拜拜~~")

            break

    (猜年龄游戏V3,最大循环10次,每3次重新循环)

    #!/usr/bin/env  python

    #-*-coding:utf-8 -*-

    #Author: ZhangJiangHua

    age = 19

    counter =0

    for i in range(10):

        print('--counter:',counter)

        if counter <3:

            guess_num = int(input("input your guess num:"))

            if guess_num == age :

                print("恭喜你,猜对了!")

                break  #不往后走了,跳出整个循环

            elif guess_num >age:

                print("往小了猜!")

            else:

                print("往大了猜!")

        else:

            continue_confirm = input("还要继续吗?y|n")

            if continue_confirm == 'y':

                    counter = 0

      continue   #(跳出当次循环,而不是整个)

            else:

                    print("bye")

                    break

        counter +=1    #counter = counter +1

    个人备忘,欢迎阅读
  • 相关阅读:
    Linux常用操作命令总结
    Centos7安装FastDFS教程
    Permutation Sequence
    Next Permutation
    Remove Element
    4Sum
    3Sum Closest
    3Sum
    Longest Consecutive Sequence
    Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/zhangjianghua/p/5697731.html
Copyright © 2020-2023  润新知