zoukankan      html  css  js  c++  java
  • python的循环和选择

    一.python的选择结构:

      python的选择结构有两种选择结构一种是单选择(if...else)另一种则是多选择结构(if ...elif...elif)

      下面用代码来实现:

      1.if....else

        结构:

            if boolean :

              语句1

              语句2

            else :

              语句3

    from datetime import datetime #在datetime标准库中导入datetime


    odd =[1,3,5,7,9,11,13,15,17,19,21,23]

    if  datetime.today().hour in odd:#in是python的运算符,表是对象是否在后面的列表中
        print(datetime.today().hour)
        print("now the time is odd")
    else:
        print(datetime.today().hour)
        print("now the time is even")
            

    在上面的简单代码中我们可以看到python的格式与其他的高级语言程序不同,它没有大括号来区分代码块,对于python区分代码块都是用:和制表符来区分
    且需要说明的是python中一旦缺少制表符和:上述代码就会出错,同时我们可以看出python不需要我们去定义变量而可以直接使用

    2.if...elif...elif

      结构

        if boolean :

          语句1

          语句2

        elif boolean:

          ....

    score=int(input("请输入你的成绩:"))#python的输入函数并将输入转化为整数
    if(score>100 or score <0):
        print("输入错误")
    elif 100>=score >=90:
        print("你的成绩是A")
    elif 90>score>=80:
        print("你的成绩是B")
    elif 80>=score>=60:
        print("你的成绩是C")
    elif score<60:
        print("你的成绩是D")
            

    同于其他语言python 并没有if else if的结构且我们可以看出的是python可以像数学中那样列不等式所以更方便初学者来学习

    二.python的循环结构

    python的循环有两种一种是while 另一种则是for

      1.while循环:

      while循环一般用于某个条件不成立时使用

        结构:while boolean :

              语句

        

    #用while循环来计算阶乘
    while True:
        i=input("请输入想要计算的数:")
        if  not i.isdigit(): #判断用户输入的是否是纯数字
            print("请输入纯数字") 
            continue #不是时终止当前循环,并开启下一次循环
        else:
            if int(i)<0:
                print("请输入正数")
                continue
            else : #计算阶乘
                n=int(i)
                sum=0
                for temp in range(1,n+1):#range函数为内置函数表示在某个范围相当于 1=<temp<n+1
                    
                    facto=1 #保存每个中间数的阶乘
                    for temp2 in range(1,temp+1):
                        facto=facto*temp2
                        
                    sum=sum+facto
                print("1!+2!+3!.....+n!={0}".format(sum)) #format是字符串匹配的函数{0}是表示format中要配对的位置例如{1} format(1,2,3)则匹配的是2
               
                temp=input("请问要接着计算吗?(y/n)") ;
                if temp=='y':
                    continue
                else:
                    break #break用于终止当前循环

    2.for循环

      for循环和while循环一样都时重复的执行某个语句,但不同的是for循环是利用迭代来穷历序列中的所有元素直到序列中没有元素为止

    结构:

       for i(元素) in item(某个序列):

        语句

    #九九乘法表
    for i in range(1,10):
        for j in range(1,i+1):
            print("{1}*{0}={2}".format(i,j,i*j),end=" ")
        print("")

    ---恢复内容结束---

  • 相关阅读:
    牛客 Wannafly 挑战赛26D 禁书目录 排列组合 概率期望
    UOJ#269. 【清华集训2016】如何优雅地求和
    斯特林数 学习笔记
    AtCoder Grand Contest 006 (AGC006) C
    Codeforces 1045D Interstellar battle 概率期望
    Codeforces 1045A Last chance 网络流,线段树,线段树优化建图
    Codeforces 1053C Putting Boxes Together 树状数组
    Codeforces 109D String Transformation 字符串 哈希 KMP
    AtCoder Grand Contest 1~10 做题小记
    AtCoder Grand Contest 002 (AGC002) F
  • 原文地址:https://www.cnblogs.com/sank/p/10085082.html
Copyright © 2011-2022 走看看