我创建了一个投票计算器,但一行代码似乎把另一部分弄乱了。是我的变量吗?
I've created a voting calculator, but one line of code seems to mess up another section. Is it my variables?
我的 python class 有一个相当复杂的(对我来说,但对 python 专家来说可能很简单)的代码,用于分析地方选举:它计算选票然后打印结果(候选人+选票,县+选票,选举获胜者)。我得出的结果是正确的……除了一件,我在代码中用“# !Something in this line is causing the Winner/Winning Vote Count and Winning Percentage to get thrown out !”使用前面的代码行,不会打印下面的选举获胜者结果(该部分会打印,但不会打印获胜者的姓名和投票总数)。如果我删除那段代码,错误的县被识别为“得票最多的县”(largest_county;阿拉帕霍而不是丹佛,丹佛是得票最多的正确县),但随后获胜者选举的打印正确。
打印这些信息的两部分代码——得票最多的县和选举的获胜者——实际上是相同的代码,只是变量不同。我想知道我在那里声明的某个变量是否可能加倍或增加两次,但我无法弄清楚它可能是什么。也许是“投票”变量?任何帮助将不胜感激。
# snip some code
# Candidate Options list and candidate votes dictionary.
candidate_options = []
candidate_votes = {}
# 1: Create a county list and county votes dictionary.
counties = []
county_votes = {}
# Track the winning candidate, vote count and percentage
winning_candidate = ""
winning_count = 0
winning_percentage = 0
# 2: Track the largest county and county voter turnout.
largest_county = ""
largest_count = 0
largest_percentage = 0
# snip some code
if candidate_name not in candidate_options:
# Add the candidate name to the candidate list.
candidate_options.append(candidate_name)
# And begin tracking that candidate's voter count.
candidate_votes[candidate_name] = 0
# Add a vote to that candidate's count
candidate_votes[candidate_name] += 1
# 4a: Write a decision statement that checks that the county does not match any existing county in the county list.
if county_name not in counties:
# 4b: Add the existing county to the list of counties.
counties.append(county_name)
# 4c: Begin tracking the county's vote count.
county_votes[county_name] = 0
# 5: Add a vote to that county's vote count.
county_votes[county_name] += 1
# snip some code
winning_count = votes
largest_county = county_name
largest_percentage = vote_percentage
# 7: Print the county with the largest turnout to the terminal.
largest_county_summary = (
f"-------------------------\n"
f"County with largest turnout: {largest_county}\n"
f"-------------------------\n")
print(largest_county_summary)
# snip some code
# Determine winning vote count, winning percentage, and candidate.
if (votes > winning_count) and (vote_percentage > winning_percentage):
winning_count = votes
winning_candidate = candidate_name
winning_percentage = vote_percentage
# snip some code
好像错误在这里:
if (votes > largest_count) and (vote_percentage > largest_percentage):
winning_count = votes
largest_county = county_name
largest_percentage = vote_percentage
而不是 winning_count = votes
,您应该分配 largest_count = votes
我的 python class 有一个相当复杂的(对我来说,但对 python 专家来说可能很简单)的代码,用于分析地方选举:它计算选票然后打印结果(候选人+选票,县+选票,选举获胜者)。我得出的结果是正确的……除了一件,我在代码中用“# !Something in this line is causing the Winner/Winning Vote Count and Winning Percentage to get thrown out !”使用前面的代码行,不会打印下面的选举获胜者结果(该部分会打印,但不会打印获胜者的姓名和投票总数)。如果我删除那段代码,错误的县被识别为“得票最多的县”(largest_county;阿拉帕霍而不是丹佛,丹佛是得票最多的正确县),但随后获胜者选举的打印正确。
打印这些信息的两部分代码——得票最多的县和选举的获胜者——实际上是相同的代码,只是变量不同。我想知道我在那里声明的某个变量是否可能加倍或增加两次,但我无法弄清楚它可能是什么。也许是“投票”变量?任何帮助将不胜感激。
# snip some code
# Candidate Options list and candidate votes dictionary.
candidate_options = []
candidate_votes = {}
# 1: Create a county list and county votes dictionary.
counties = []
county_votes = {}
# Track the winning candidate, vote count and percentage
winning_candidate = ""
winning_count = 0
winning_percentage = 0
# 2: Track the largest county and county voter turnout.
largest_county = ""
largest_count = 0
largest_percentage = 0
# snip some code
if candidate_name not in candidate_options:
# Add the candidate name to the candidate list.
candidate_options.append(candidate_name)
# And begin tracking that candidate's voter count.
candidate_votes[candidate_name] = 0
# Add a vote to that candidate's count
candidate_votes[candidate_name] += 1
# 4a: Write a decision statement that checks that the county does not match any existing county in the county list.
if county_name not in counties:
# 4b: Add the existing county to the list of counties.
counties.append(county_name)
# 4c: Begin tracking the county's vote count.
county_votes[county_name] = 0
# 5: Add a vote to that county's vote count.
county_votes[county_name] += 1
# snip some code
winning_count = votes
largest_county = county_name
largest_percentage = vote_percentage
# 7: Print the county with the largest turnout to the terminal.
largest_county_summary = (
f"-------------------------\n"
f"County with largest turnout: {largest_county}\n"
f"-------------------------\n")
print(largest_county_summary)
# snip some code
# Determine winning vote count, winning percentage, and candidate.
if (votes > winning_count) and (vote_percentage > winning_percentage):
winning_count = votes
winning_candidate = candidate_name
winning_percentage = vote_percentage
# snip some code
好像错误在这里:
if (votes > largest_count) and (vote_percentage > largest_percentage):
winning_count = votes
largest_county = county_name
largest_percentage = vote_percentage
而不是 winning_count = votes
,您应该分配 largest_count = votes