将条件实现到数组中

我正在尝试在我的程序中实现另一个条件,但似乎无法弄清楚。


这是代码:


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.")

如果两个人在某一年的薪水完全相同,您应该打印一个错误并让用户再次输入该年的薪水。(条件是工资不应该完全相同)。


我期待大家的反馈。


先感谢您。


噜噜哒
浏览 84回答 2
2回答

不负相思意

好的,所以在这里建立 Surge10 可以更完整地替换您的代码。我将所有部分分解为函数,并制作了一个字典作为我的内存数据库。total_years = 5years = {}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 Truedef get_user_totals(userId):    total = 0    for key, value in years.items():        if ':{}'.format(userId) in key:            total += value    return totalfor 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 = FalseuserOneTotal = 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.")

守着一只汪

因为我们检查了五个不同的年份,所以我对这五年使用了一个循环。稍后我们可以将它恢复到各个年份。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 = Falseyear_one = years[0]year_two = years[1]...print(year_one)print(year_two)...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python