• python核心编程第二章习题


    """
    python核心编程练习2
    使用的python版本: Python 3.2.3
    """
    
    def t2_5():
        """分别写一个while和for循环,输出整数0~10。
        """
        i = 0
        while i <= 10:
            print(i,'', end='')
            i += 1
        print()
    
        for i in range(0, 11):
            print(i,'', end='')
        print()
    
    def t2_6():
        """条件判断。
    
            判断一个数是整数,负数,或者等于0.
        """
        try:
            anum = int(input('Enter a number: '))
        except ValueError:
            print('what you entered is not a number!!!')
        else:
            print(anum)
    
    def t2_7():
        """输入一个字符串,逐个字符输出该字符串。使用while和for
        """
        astr = input('Enter a string: ')
        for eachar in astr:        print(eachar, end='')
        print()
    
        strlen = len(astr)
        i = 0
        while i < strlen:
            print(astr[i], end='')
            i += 1
        print()
    
    def t2_8():
        """输入5个数,分别存入list和tuple,算出他们的和
        """
        alist = []
        atuple = ()
        for i in range(0, 5):
            num = int(input('Enter a number: '))
            alist.append(num)
            atuple = atuple + (num, )
        
        total = 0
        for num in alist:
            total += num
        print(total)
    
        total = 0
        for num in atuple:
            total += int(num)
        print(total)
    
    def t2_9():
        """输入5个数,存入list和tuple,算出他们的平均值
    
            在python3.2版本中,单除号/实现的是小数除法,可以直接用,不过这里考虑
            输入的数可能为浮点数,所以也用float对输入进行转换
            如果在2.x版本中,如果输入的是整数,需要先将数转成float,再除
        """
        alist = []
        atuple = ()
    
        # input 5 numbers
        for i in range(0, 5):
            num = float(input('Enter a number: '))
            alist.append(num)
            atuple = atuple + (num, ) # 向一个tuple添加元素,要使用tuple相加
                                      # 所以这里先办单个数num组成一个只有一个
                                      # 元素的tuple再与原来的tuple相加
        total = 0
        for num in alist:
           total += num
        print(total/len(alist))
    
        total = 0
        for num in atuple:
            total += num
        print(total/len(atuple))
    
    def t2_10():
        """让用户输入一个1~100之间的数,如果用户输入的数满足条件,
        显示成功并推出,否则显示错误信息,然后再次提示用户输入。
        """
        prompt = 'Please input a number (1~100): '
        while True:
            try:
                num = int(input(prompt))
            except ValueError:
                print('Not a number')
            else:
                if 1 <= num <= 100:
                    break
                else:
                    print(num, 'not correct')
        print('Yes, you got it.')
    
    def t2_11():
        """带文本菜单的程序。
    
            菜单如下(1)取5个数的和 (2)取5个数的平均值 (X)退出
        """
        prompts = '''
    (1)sum of five numbers.
    (2)average of five numbers.
    (X)Quit
    Enter your choice: '''
    
        while True:
            choice = input(prompts).strip()[0].lower()
            print('\nYou picked [{}]'.format(choice))
    
            if choice not in '12x':
                print('Invalid options:', choice)
            elif choice == '1':
                t2_8()
            elif choice == '2':
                t2_9()
            else:
                break
    
    def t2_15():
        """用户输入三个数,保存至不同的变量中。不使用列表或排序算法,
        自己写代码来对这三个数进行排序:从大到小,从小到大
        """
        aa = int(input('Enter a number: '))
        bb = int(input('Enter a number: '))
        cc = int(input('Enter a number: '))
       
        a, b, c = aa, bb, cc
        # from smallest to largest
        if a > b:
            a, b = b, a # swap two value
        if a > c:
            a, c = c, a
        if b > c:
            b, c = c, b
        print('{} < {} < {}'.format(a, b, c))
    
        # from largest to smallest
        a, b, c = aa, bb, cc
        if a < b:
            a, b = b, a
        if a < c:
            a, c = c, a
        if b < c:
            b, c = c, b
        print('{} > {} > {}'.format(a, b, c))
    
    def t2_16():
        """输入一个文件名,然后读取该文件,并将文件内容输入到屏幕上
        """
        filename = input('Enter file name: ')
        fobj = open(filename, 'r')
        for eachline in fobj:
            print(eachline, end='') # 默认的print语句会加换行,这里把换行去掉
        fobj.close()
        
    
    if __name__ == '__main__':
        #t2_5()
        #t2_6()
        #t2_7()
        #t2_8()
        #t2_9()
        #t2_10()
        #t2_11()
        #t2_15()
        #t2_16()
    



  • 相关阅读:
    android之Animation
    A. Puzzles CodeForces Round #196 (Div.2)
    HDU 4662 MU Puzzle 2013 Multi-University Training Contest 6
    UVa 11464 Even Parity 偶数矩阵
    LA 3635 Pie 派 NWERC 2006
    UVa 11520 Fill the Square 填充正方形
    UVa 11384 Help is needed for Dexter 正整数序列
    HDU 4639 Hehe 2013 Multi-University Training Contest 4
    HDU 4627 The Unsolvable Problem 2013 Multi-University Training Contest 3
    HDU 4608 I-number 2013 Multi-University Training Contest 1
  • 原文地址:https://www.cnblogs.com/huiqin/p/3674849.html
Copyright © 2020-2023  润新知