这是我有多远,但我不知道如何再次调用该程序:
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!')
答案 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)
一些评论:
new_salary is 200000
检查身份(两件事是否是同一个对象)而不是相等(两件事是否都有相同的值);你想要new_salary == 200000
。new_salary
就像你从input
得到的一切一样,是一个字符串;您可以使用new_salary = int(input(...))
修复此问题。Chelsea_Salaries_2014
中的薪水;为此,请将新值分配回字典Chelsea_Salaries_2014[search_input][1] = new_salary
。<= 200000
,但是您永远不会检查<= 100000
。我想你想要这样的东西:
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
始终是有效密钥吗?如果他们只输入部分姓名怎么办?