• python之控制流习题+代码


     1 # !/usr/bin/python3
     2 # -*- coding: utf-8 -*-
     3 # @Time     :2018/11/26 15:32
     4 # @Author   :yosef
     5 # @Email    :wurz529@foxmail.com
     6 # @File:    :class12.py
     7 # @Software :PyCharm Community Edition
     8 '''
     9 for + range 完成1-100累加
    10 '''
    11 sum1 = 0;
    12 for item in range(1, 101):
    13     sum1 += item
    14 print(sum1)
    15 
    16 """
    17 [5,6,7,9,10,23,45]列表倒序输出
    18 """
    19 a = [5, 6, 7, 9, 10, 23, 45]
    20 for i in range(len(a)):    # len(a)代表长度,range得出 0123456
    21     print(a[len(a)-i-1], end=" ")
    22 print("
    ")
    23 
    24 '''
    25 商场降价促销,买50-100(即[50, 100]),会给10%优惠,如果购买金额大于100,则会给20%优惠。设计程序,询问价格且给出最终金额
    26 '''
    27 while True:
    28     sale = int(input("请问商品的总价是:"))
    29     if sale>=0 and sale<50:
    30         print("商品不打折哦")
    31         break
    32     elif sale>=50 and sale<=100:
    33         print("商品打了9折,最终价格为:%.2f" % (0.9*sale))
    34         break
    35     elif sale > 100:
    36         print("商品打了8折,最终价格为:%.2f" % (0.8*sale))
    37         break
    38     else:
    39         print("亲,请重新输入哦~")
    40         continue
    41 print("
    ")
    42 
    43 """
    44 女足足球队寻找年龄20-22岁的女孩,设计程序,m表示男,f表示女。用户输入性别年龄,查看是否能加入球队,询问十次之后通国际满足条件总人数。
    45 """
    46 
    47 count = 0
    48 num = 0
    49 while num < 10:
    50     age = int(input("请输入年龄:"))
    51     sex = input("请输入性别:")
    52 
    53     if age>=20 and age<=22:
    54         if sex == "f":
    55             count += 1
    56             print("第%d个" % count)
    57         elif sex!="f" or sex!="m":
    58             print("性别输入错误")
    59     num +=1
    60     print(num)
    61 print("共有%d人符合" % count)
  • 相关阅读:
    LintCode: Climbing Stairs
    LintCode: Binary Tree Postorder Traversal
    LintCode: Binary Tree Preorder Traversal
    LintCode: Binary Tree Inorder Traversal
    Lintcode: Add Two Numbers
    Lintcode: Add Binary
    LintCode: A + B Problem
    LintCode: Remove Linked List Elements
    LintCode:Fibonacci
    Lintcode开刷
  • 原文地址:https://www.cnblogs.com/wlyhy/p/10021367.html
Copyright © 2020-2023  润新知