Python 为什么在我最后一次投票时,我的代码会一直延续到下一个if语句中?有没有更快的方法?
当我到达最后一个投票部分时,代码似乎继续到下一个if语句,我似乎无法理解为什么它不打印最后一个语句并停止?我已经彻底检查了代码,似乎找不到任何错误,尤其是最后一个函数,因为它与前三个相同。 有没有更快的方法来重复这样的代码Python 为什么在我最后一次投票时,我的代码会一直延续到下一个if语句中?有没有更快的方法?,python,python-3.x,function,loops,if-statement,Python,Python 3.x,Function,Loops,If Statement,当我到达最后一个投票部分时,代码似乎继续到下一个if语句,我似乎无法理解为什么它不打印最后一个语句并停止?我已经彻底检查了代码,似乎找不到任何错误,尤其是最后一个函数,因为它与前三个相同。 有没有更快的方法来重复这样的代码 def presvote(): with open("President.txt", "r+") as president: president = open("President.txt", "r") pres = [line.rst
def presvote():
with open("President.txt", "r+") as president:
president = open("President.txt", "r")
pres = [line.rstrip('\n', ) for line in open("President.txt")]
print("Here is a list of the people running for president: ")
print(pres)
pvote1(pres)
def pvote1(pres):
vote1 = input("Who would you like to vote for as the first choice?: ")
if vote1 in pres:
pres.remove(vote1)
print(pres)
pvote2(pres, vote1)
else:
print("That candidate is not running\nPlease enter a valid name")
pvote1(pres)
def pvote2(pres, vote1):
vote2 = input("Who would you like to vote for as the second choice?: ")
if vote2 in pres:
pres.remove(vote2)
print(pres)
pvote3(pres, vote1, vote2)
if vote2 == vote1:
print("That candidate has already been chosen, Please choose another")
pvote2(pres, vote1)
else:
print("That candidate is not running please try again")
pvote2(pres, vote1)
def pvote3(pres, vote1, vote2):
vote3 = input("Who would you like to vote for as the third choice?: ")
if vote3 in pres:
pres.remove(vote3)
print(pres)
pvote4(pres, vote1, vote2, vote3)
if vote3 == vote1 or vote2:
print("That candidate has already been chosen, Please choose another")
pvote3(pres, vote1, vote2)
else:
print("That candidate is not running please try again")
pvote3(pres, vote1, vote2)
def pvote4(pres, vote1, vote2, vote3):
vote4 = input("Who would you like to vote for as the fourth choice?: ")
if vote4 in pres:
pres.remove(vote4)
print(pres)
# print("Here is the list of your chosen candidates:\n",vote1,"\n",vote2,"\n",vote3,"\n",vote4)
if vote4 == vote1 or vote2 or vote3:
print("That candidate has already been chosen, Please choose another")
pvote4(pres, vote1, vote2, vote3)
else:
print("That candidate is not running please try again")
pvote4(pres, vote1, vote2, vote3)
presvote()```
或
语句可能是导致问题的原因
如果vote4==vote1或vote2或vote3
应写成:
if vote4 == vote1 or vote4 == vote2 or vote4 == vote3
如果vote3==vote1或vote2
为了提高效率,我下载了influct
库,以获得投票位置(即“第一”、“第二”、“第三”)
导入屈折
def presvote():
#以open(“President.txt”、“r+”)为主席:
president=open(“president.txt”、“r”)
pres=[line.rstrip('\n',)用于打开的行(“President.txt”)]
打印(“这是总统竞选人的名单:”)
打印(pres)
投票=投票(主席)
打印(投票)
def投票(主席):
票数=[]
p=拐点引擎()#启动拐点引擎
虽然len(Voces)@Nebulous29已经触及了一个重大问题,也许它真的不值得再解释一遍(因为这个概念已经在无数其他线程中被解释得透不过气来),但无论如何,这里有一个复习
在Python中,字符串(包括其他内置集合,如列表)具有隐式的“truthyness”。这意味着,当您要求将字符串计算为布尔值(True或False)时,无论是隐式还是显式,如果字符串为非空,它将计算为True
,否则将计算为False
(如果为空。列表具有类似的行为)
请注意,字符串literal“
是唯一计算结果为False
的字符串,因为它是空的。所有其他字符串都不是空的。有人会说这个字符串是“假的”
这让我们可以做这样的事情:
>>> string = "Hello"
>>> if string:
print("We're in!")
We're in!
字符串“Hello”
隐式地是“truthy”,因为它不是空的,所以我们输入if语句的主体并执行print语句
更进一步(这就是代码出错的地方):
字符串是“Hello”
,它既不是“Something”
也不是“Blah”
,但我们仍然输入if语句的主体,那么给出了什么呢?让我们按照Python所看到的方式计算这个布尔表达式:
string==“Something”
变为False
,因为string
不是
等于“某物”
“Blah”
变为True
,因为字符串literal“Blah”
是
非空,因此真实if False or True:
它的计算结果为True
,因为只有or操作的一个参数需要为True才能使输出为True
有没有更快的方法来重复这样的代码
很好,你正在使用函数!然而,函数是为了减少您编写的代码量,然而,您的四个函数实际上做了相同的事情
这四个有问题的函数还使用递归来模拟循环,这是一个坏主意。对于您编写的任何以这种方式使用递归的程序,我可以给您一个会引发递归错误的输入。您最好只使用循环:
def main():
candidates = [
"Bob",
"Sam",
"Tom",
"Barry"
]
print("Here are all the candidates running for president:")
print(candidates)
vote_number_names = [
"first",
"second",
"third",
"fourth"
]
selected_candidates = []
for vote_number in vote_number_names:
while True:
candidate = input(f"Who is your {vote_number} choice?: ")
if candidate not in candidates:
print("That candidate is not running.")
elif candidate in selected_candidates:
print("That candidate has already been selected.")
else:
break
selected_candidates.append(candidate)
print("Here are the candidates, in the order you picked:")
print(selected_candidates)
main()
输出:
Here are all the candidates running for president:
['Bob', 'Sam', 'Tom', 'Barry']
Who is your first choice?: Bob
Who is your second choice?: jqedfkljdla
That candidate is not running.
Who is your second choice?: Bob
That candidate has already been selected.
Who is your second choice?: Tom
Who is your third choice?: Barry
Who is your fourth choice?: Sam
Here are the candidates, in the order you picked:
['Bob', 'Tom', 'Barry', 'Sam']
>>>
这回答了你的问题吗?为什么要在
presvate()
中打开同一个文件3次?谢谢,这对回答我的问题很有帮助
if False or True:
def main():
candidates = [
"Bob",
"Sam",
"Tom",
"Barry"
]
print("Here are all the candidates running for president:")
print(candidates)
vote_number_names = [
"first",
"second",
"third",
"fourth"
]
selected_candidates = []
for vote_number in vote_number_names:
while True:
candidate = input(f"Who is your {vote_number} choice?: ")
if candidate not in candidates:
print("That candidate is not running.")
elif candidate in selected_candidates:
print("That candidate has already been selected.")
else:
break
selected_candidates.append(candidate)
print("Here are the candidates, in the order you picked:")
print(selected_candidates)
main()
Here are all the candidates running for president:
['Bob', 'Sam', 'Tom', 'Barry']
Who is your first choice?: Bob
Who is your second choice?: jqedfkljdla
That candidate is not running.
Who is your second choice?: Bob
That candidate has already been selected.
Who is your second choice?: Tom
Who is your third choice?: Barry
Who is your fourth choice?: Sam
Here are the candidates, in the order you picked:
['Bob', 'Tom', 'Barry', 'Sam']
>>>