亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將條件實現到數組中

將條件實現到數組中

嚕嚕噠 2022-10-25 15:06:33
我正在嘗試在我的程序中實現另一個條件,但似乎無法弄清楚。這是代碼:print ("Welcome to the winning card program.")year_one=[]year_one.append(eval(input("Enter the salary individual 1 got in year 1:  ")))year_one.append(eval(input("Enter the salary individual 2 got in year 1:  "))) if year_one[0]==year_one[1]:   print("Error. The amount are the same, please reenter information.")year_two=[]year_two.append(eval(input("Enter the salary individual 1 got in year 2:  ")))year_two.append(eval(input("Enter the salary individual 2 got in year 2:  "))) if year_two[0]==year_two[1]:   print("Error. The amount are the same, please reenter information.")year_three=[]year_three.append(eval(input("Enter the salary individual 1 got in year 3:  ")))year_three.append(eval(input("Enter the salary individual 2 got in year 3:  ")))  if year_three[0]==year_three[1]:   print("Error. The amount are the same, please reenter information.")year_four=[]year_four.append(eval(input("Enter the salary individual 1 got in year 4:  ")))year_four.append(eval(input("Enter the salary individual 2 got in year 4:  "))) if year_four[0]==year_four[1]:  print("Error. The amount are the same, please reenter information.")year_five=[]year_five.append(eval(input("Enter the salary individual 1 got in year 4:  ")))year_five.append(eval(input("Enter the salary individual 2 got in year 4:  "))) if year_five[0]==year_five[1]:  print("Error. The amount are the same, please reenter information.")individual1_total=year_one[0]+year_two[0]+year_three[0]+year_four[0]+year_five[0]individual2_total=year_one[1]+year_two[1]+year_three[1]+year_four[1]+year_five[1] if (individual1_total>individual2_total):  print("Individual one has the highest salary.") elif (individual2_total>individual1_total):  print("Individual two has the highest salary.")如果兩個人在某一年的薪水完全相同,您應該打印一個錯誤并讓用戶再次輸入該年的薪水。(條件是工資不應該完全相同)。我期待大家的反饋。先感謝您。
查看完整描述

2 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

好的,所以在這里建立 Surge10 可以更完整地替換您的代碼。我將所有部分分解為函數,并制作了一個字典作為我的內存數據庫。


total_years = 5

years = {}


def dict_key(year, userId):

    return '{}:{}'.format(year, userId)


def get_user_data(year, userId):

    key = dict_key(year, userId)

    years[key] = float(input("Enter the salary individual {} got in year {}:".format(userId, year)))


def get_year_salaray(year, userId):

    key = dict_key(year, userId)

    return years[key]


def any_salaries_match(year, userIds):

    for userLeft in userIds:

        salary_left = get_year_salaray(year, userLeft)

        for userRight in userIds:

            if userLeft != userRight:

                salary_right = get_year_salaray(year, userRight)

                if salary_left == salary_right:

                    return True


def get_user_totals(userId):

    total = 0

    for key, value in years.items():

        if ':{}'.format(userId) in key:

            total += value

    return total


for x in range(total_years):

    year = x + 1

    data_invalid = True

    while data_invalid:

        userOne = 1

        userTwo = 2

        get_user_data(year, userOne)

        get_user_data(year, userTwo)


        if any_salaries_match(year, [userOne, userTwo]):

            print("Error. The amount are the same, please reenter information.")

        else:

            data_invalid = False


userOneTotal = get_user_totals(1)

userTwoTotal = get_user_totals(2)


if (userOneTotal>userTwoTotal):

  print("Individual one has the highest salary.")

elif (userTwoTotal>userOneTotal):

  print("Individual two has the highest salary.")


查看完整回答
反對 回復 2022-10-25
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

因為我們檢查了五個不同的年份,所以我對這五年使用了一個循環。稍后我們可以將它恢復到各個年份。


print ("Welcome to the winning card program.")

years=[]


for x in range(5):


    # range starts at zero so add one all the time

    # to start it at one

    y = str(x + 1)


    errors = True

    while errors:


        salary_one = input("Enter the salary individual 1 got in year " + y + ":  ")

        salary_two = input("Enter the salary individual 2 got in year " + y + ":  ")


        if salary_one == salary_two:

            print("Error. The amount are the same, please reenter information.")

            errors = True

        else:

            years.append([salary_one, salary_two])

            errors = False


year_one = years[0]

year_two = years[1]


...

print(year_one)

print(year_two)

...


查看完整回答
反對 回復 2022-10-25
  • 2 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號