Python 输入的数据成为for循环中的新数据
长话短说,我和我的朋友开了个玩笑。每当华盛顿足球队击败一支球队时,该队就成为“足球队”。第一周,足球队击败了老鹰队,所以现在他们被称为费城足球队。我被迷住了,我决定制作一个python程序来实现这一点Python 输入的数据成为for循环中的新数据,python,loops,conditional-statements,Python,Loops,Conditional Statements,长话短说,我和我的朋友开了个玩笑。每当华盛顿足球队击败一支球队时,该队就成为“足球队”。第一周,足球队击败了老鹰队,所以现在他们被称为费城足球队。我被迷住了,我决定制作一个python程序来实现这一点 for i in range(17): city = "Washington" football_team = city + " Football Team" answer = input("Did the " +
for i in range(17):
city = "Washington"
football_team = city + " Football Team"
answer = input("Did the " + football_team + " win today? ")
if answer == "No":
print("The team is the " + football_team)
if answer == "Yes":
city = input("Who did the " + football_team + " defeat? ")
football_team = city + " Football Team"
print("The new football team is the " + football_team)
每当用户输入答案yes并为足球队输入一个新城市时,循环返回到顶部,其中:
city = "Washington"
我希望我能找到一个方法一旦代码重申,这个城市的名字不是华盛顿。我明白这在代码中有什么意义,但我无法确定如何让这座城市改名。如果用户回答“是”,并输入一个新的足球队名称,代码将按照我希望的方式工作,直到它再次出现。如何使新输入的数据成为该代码的当前原始数据?在循环外部定义变量“city”。根据您的需要,可能有两种解决方案。如果这样的说法——“只要华盛顿足球队击败一支球队,该队就成为“足球队”。第一周,足球队击败了老鹰队,因此现在他们被称为费城足球队”——对新球队也是如此(如果老鹰队击败了某个钢人队,那么他们现在被称为匹兹堡足球队)然后选择解决方案1。 解决方案1
for i in range(5):
if i==0:
city = "Washington"
football_team = city + " Football Team"
answer = input("Did the " + football_team + " win today? ")
if answer == "No":
print("The team is the " + football_team)
if answer == "Yes":
city = input("Who did the " + football_team + " defeat? ")
football_team = city + " Football Team"
print("The new football team is the " + football_team)
另一方面,如果标准始终是华盛顿足球队,那么选择解决方案2。
解决方案2:
for i in range(5):
if i==0:
city = "Washington"
football_team = city + " Football Team"
answer = input("Did the " + "Washington Football Team" + " win today? ")
if answer == "No":
print("The team is the " + football_team)
if answer == "Yes":
city = input("Who did the " + "Washington Football Team" + " defeat? ")
football_team = city + " Football Team"
print("The new football team is the " + football_team)
Week 1
Washington defeated Philadelphia with the score of 27 - 17.
The football team is still the Washington Football Team.
Week 2
Arizona defeated Washington with the score of 30 - 15.
The new football team is the Arizona Football Team.
Week 3
Detroit defeated Arizona with the score of 26 - 23.
The new football team is the Detroit Football Team.
Week 4
New Orleans defeated Detroit with the score of 35 - 29.
The new football team is the New Orleans Football Team.
如上所述,您需要将
'Washington'
初始化为城市,然后可以根据谁获胜进行更新
所以这看起来很有趣,我也尝试过通过检查分数来实现自动化:
import requests
import operator
def diffr(items):
return items[0] - sum(items[1:])
football_team_dict = {'city':'Washington','team':'Football Team'}
for i in range(1,18):
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard?week=%s' %i
jsonData = requests.get(url).json()['events']
#Get Football Team game
for game in jsonData:
if football_team_dict['city'] in game['name']:
break
if game['status']['type']['completed'] == True:
print('Week %s'%i)
#Check if Football Team won
score_dict = {}
for team in game['competitions'][0]['competitors']:
score_dict.update({team['team']['location']:int(team['score'])})
winCity = max(score_dict.items(), key=operator.itemgetter(1))[0]
lossCity = min(score_dict.items(), key=operator.itemgetter(1))[0]
if diffr(list(score_dict.values())) == 0:
print ('%s tied %s with the score of %s - %s.' %(game['competitions'][0]['competitors'][0]['team']['location'], game['competitions'][0]['competitors'][1]['team']['location'], score_dict[winCity], score_dict[lossCity]))
winCity = football_team_dict['city']
else:
print ('%s defeated %s with the score of %s - %s.' %(winCity, lossCity, score_dict[winCity], score_dict[lossCity]))
if winCity == football_team_dict['city']:
print ('The football team is still the %s Football Team.\n' %football_team_dict['city'])
else:
football_team_dict.update({'city':winCity})
print ('The new football team is the %s Football Team.\n' %football_team_dict['city'])
输出:
for i in range(5):
if i==0:
city = "Washington"
football_team = city + " Football Team"
answer = input("Did the " + "Washington Football Team" + " win today? ")
if answer == "No":
print("The team is the " + football_team)
if answer == "Yes":
city = input("Who did the " + "Washington Football Team" + " defeat? ")
football_team = city + " Football Team"
print("The new football team is the " + football_team)
Week 1
Washington defeated Philadelphia with the score of 27 - 17.
The football team is still the Washington Football Team.
Week 2
Arizona defeated Washington with the score of 30 - 15.
The new football team is the Arizona Football Team.
Week 3
Detroit defeated Arizona with the score of 26 - 23.
The new football team is the Detroit Football Team.
Week 4
New Orleans defeated Detroit with the score of 35 - 29.
The new football team is the New Orleans Football Team.
您可以在循环之外定义变量。解决方案1适合我。现在看一下使用i的if语句,它对我来说非常有意义!非常感谢。哇,你真的比我走得更远!