gpt4 book ai didi

python - 将工资从一维数组转换为二维数组

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:29 24 4
gpt4 key购买 nike

我创建了一个工资系统,用于计算工资总额、税金和 Netty 。我可以让它从员工列表中创建一个一维数组。输出每位工作人员的总税额和 Netty 。我想要回答的主要问题(第一个问题)是。

  1. 主要是我想将一维数组转换为二维数组。 (我不想使用 NUMPY)

如果可以的话您能解释一下下面的两点

  • 当我连接小时和工作时间时,当我使用逗号时会出现错误,但当我使用 + 时不会出现错误,这是为什么

  • 如何对输出进行编码,以便它显示货币的值(value),例如 $

  • 这是我的输出 atm

    output -: ['Kyle', '3,025.00', '605.00', '2,420.00', 'John', '3,025.00', '605.00', '2,420.00', 'Peter', '3,025.00', '605.00', '2,420.00', 'Harry', '3,025.00', '605.00', '2,420.00']

    谢谢

    我尝试使用嵌套 for 循环来转换 1darray,但我不是高级编码员,无法实现此目的

    #wages system to work out gross tax and net
    #me
    # #16/9/2019

    #this is 20 percent of the gross for tax purposes
    taxrate=0.2
    #blank list

    mylistwage=[]

    def calc(i):
    #input how much paid per hour joined with first iteration of for loop ie
    #persons name
    pph = float(input("how much does "+ i+ " get paid an hour"))
    #as above but different question
    hw = float(input("how many hours did "+ i+" work this week"))
    #calculates the weekly gross wage
    gross=hw*pph
    #calculates tax needed to be paid
    tax=gross*taxrate
    #calculates how much your take home pay is
    net=gross-tax
    #adds the gross tax and wage to a one dimentionsal list
    mylistwage.append(i)
    #formats appended list so that it is to 2 decimal float appends 3 values
    mylistwage.append(format(gross,",.2f"))
    mylistwage.append(format(tax,",.2f"))
    mylistwage.append(format(net,",.2f"))



    mylist=["Kyle","John","Peter","Harry"]
    for i in (mylist):
    calc(i)
    print(mylistwage)

    最佳答案

    一种方法是在 calc 方法中创建一个本地列表,然后添加必须添加到该列表中的任何信息并返回该列表。

    现在,在 for 循环中迭代 mylist 时,每次只需将其附加到 mainlistwage 变量中。我建议对代码进行以下更改:添加 CAP 注释作为说明。

    #wages system to work out gross tax and net
    #me
    # #16/9/2019

    #this is 20 percent of the gross for tax purposes
    taxrate=0.2
    #blank list

    mainlistwage=[] #CHANGING VARIABLE NAME HERE

    def calc(i):
    mylistwage = [] #CREATING LOCAL VARIABLE
    #input how much paid per hour joined with first iteration of for loop ie
    #persons name
    pph = float(input("how much does "+ i+ " get paid an hour"))
    #as above but different question
    hw = float(input("how many hours did "+ i+" work this week"))
    #calculates the weekly gross wage
    gross=hw*pph
    #calculates tax needed to be paid
    tax=gross*taxrate
    #calculates how much your take home pay is
    net=gross-tax
    #adds the gross tax and wage to a one dimentionsal list
    mylistwage.append(i)
    #formats appended list so that it is to 2 decimal float appends 3 values
    mylistwage.append(format(gross,",.2f"))
    mylistwage.append(format(tax,",.2f"))
    mylistwage.append(format(net,",.2f"))
    return mylistwage # ADDING RETURN STATEMENT


    mylist=["Kyle","John","Peter","Harry"]
    for i in (mylist):
    mainlistwage.append(calc(i)) # APPENDING EACH RETURN LIST TO MAINLIST VARIABLE
    print(mainlistwage)

    关于python - 将工资从一维数组转换为二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58004079/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com