美文网首页
Python 100练习题 Day5

Python 100练习题 Day5

作者: P酱不想说话 | 来源:发表于2021-03-18 23:14 被阅读0次

    Question 16

    Question:
    Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers. >Suppose the following input is supplied to the program:

    1,2,3,4,5,6,7,8,9
    Then, the output should be:

    1,9,25,49,81
    Hints:
    In case of input data being supplied to the question, it should be assumed to be a console input.

    num = input().split(",")
    odd_num = []
    for i in range(0,len(num)):
        if not int(i)%2==0:
            odd_num.append(i*i)
    print(odd_num)
    

    Question 17

    Question:
    Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:

    D 100
    W 200
    D means deposit while W means withdrawal.
    Suppose the following input is supplied to the program:

    D 300
    D 300
    W 200
    D 100
    Then, the output should be:

    500
    Hints:
    In case of input data being supplied to the question, it should be assumed to be a console input.

    money = 0
    while 1:
        trans = input().split(' ')
        if trans[0] == 'D':
            money = money + int(trans[1])
        elif trans[0] == 'W':
            money = money - int(trans[1])
        elif input() == '':
            break
        print(f'Your current balance is: {money}')
    

    相关文章

      网友评论

          本文标题:Python 100练习题 Day5

          本文链接:https://www.haomeiwen.com/subject/xioscltx.html