编写一个程序,允许一个人输入一个球员的名字并改变他们的薪水

时间:2014-04-23 12:43:18

标签: python python-3.3

这是我有多远,但我不知道如何再次调用该程序:

Chelsea_Salaries_2014 = {'Jose Mourinho':[53, 163500, 'Unknown']}
Chelsea_Salaries_2014['Eden Hazard']=[22, 185000, 'June 2017']
Chelsea_Salaries_2014['Fernando Torres']=[29, 175000, 'June 2016']
Chelsea_Salaries_2014['John Terry']=[32, 175000, 'June 2015']
Chelsea_Salaries_2014['Frank Lampard']=[35, 125000, 'June 2014']
Chelsea_Salaries_2014['Ashley Cole']=[32, 120000, 'June 2014']
Chelsea_Salaries_2014['Petr Cech']=[31, 100000, 'June 2016']
Chelsea_Salaries_2014['Gary Cahill']=[27, 80000, 'June 2017']
Chelsea_Salaries_2014['David Luiz']=[26, 75000, 'June 2017']
Chelsea_Salaries_2014['John Obi Mikel']=[26, 75000, 'June 2017']
Chelsea_Salaries_2014['Nemanja Matic']=[25, 75000, 'June 2019']
Chelsea_Salaries_2014['Marco Van Ginkel']=[20, 30000, 'June 2018']
Chelsea_Salaries_2014['Ramires']=[26, 60000, 'June 2017']
Chelsea_Salaries_2014['Oscar']=[21, 67500, 'June 2017']
Chelsea_Salaries_2014['Lucas Piazon']=[19, 15000, 'June 2017']
Chelsea_Salaries_2014['Ryan Bertrand']=[23, 35000, 'June 2017']
Chelsea_Salaries_2014['Marko Marin']=[27, 35000, 'June 2017']
Chelsea_Salaries_2014['Cesar Azpilicueta']=[23, 55000, 'June 2017']
Chelsea_Salaries_2014['Branislav Ivanovic']=[29, 67500, 'June 2016']
Chelsea_Salaries_2014['Ross Turnbull']=[22, 17000, 'June 2017']
Chelsea_Salaries_2014['Demba Ba']=[28, 65000, 'June 2016']
Chelsea_Salaries_2014['Oriol Romeu']=[22, 15000, 'June 2015']

search_input = input('Welcome Jose. What player would you like to search for? ')
print('His Current Salary is £' + str(Chelsea_Salaries_2014[search_input][1]))
print()
new_salary = input('What would you like to change his salary to? ')

 if new_salary is 200000:
    print('Salary has been changed to £' + new_salary)
 else:
    if new_salary == >100000:
       print('This salary is ridiculous!')

2 个答案:

答案 0 :(得分:0)

试试这个:

search_input = input('Welcome Jose. What player would you like to search for? ')
print('His Current Salary is £' + str(Chelsea_Salaries_2014[search_input][1]))
print()
new_salary = int(input('What would you like to change his salary to? '))

if new_salary < 200000:
    print('Salary has been changed to £' + new_salary)
else:
    print('This salary is ridiculous!')

答案 1 :(得分:0)

一些评论:

  1. new_salary is 200000检查身份(两件事是否是同一个对象)而不是相等(两件事是否都有相同的值);你想要new_salary == 200000
  2. 这仍然行不通,因为new_salary就像你从input得到的一切一样,是一个字符串;您可以使用new_salary = int(input(...))修复此问题。
  3. 您的代码实际上并未更改字典Chelsea_Salaries_2014中的薪水;为此,请将新值分配回字典Chelsea_Salaries_2014[search_input][1] = new_salary
  4. 您要检查的是什么值?如果新值小于或等于£200,000,是否应更新该值?如果你被警告说这很荒谬,那么它应该更新吗?你需要更仔细地考虑逻辑是什么。例如,我认为您的第一次检查应该是<= 200000,但是您永远不会检查<= 100000
  5. 我想你想要这样的东西:

    def change_salary(salaries):
        search_input = input('Welcome Jose. What player would you like to search for? ')
        print('His Current Salary is £{0:,}'.format(salaries[search_input][1]))
        new_salary = int(input('What would you like to change his salary to? '))
        if new_salary <= 200000:
            salaries[search_input][1] = new_salary
            print('Salary has been changed to £{0:,}'.format(new_salary))
        else:
            print('This salary is ridiculous!')
    

    您现在可以反复调用,例如:

    while True:
        change_salary(Chelsea_Salaries_2014)
        choice = input("Go again? y/n ")
        if choice.lower() in ('n', 'no'):
            break
    

    一旦你有了这个工作,你可能想在change_salary中考虑一些输入验证 - 即用户是否总是输入new_salary的整数? search_input始终是有效密钥吗?如果他们只输入部分姓名怎么办?