在避免某个先决条件的同时执行某种循环(初学者/python)
初学者的问题是: 我正在写一个记录飞镖游戏的程序。用户输入球员及其各自的分数。可以查询一名球员的得分,并向程序询问所有球员之间的最佳总得分。我有以下职能:在避免某个先决条件的同时执行某种循环(初学者/python),python,python-3.x,Python,Python 3.x,初学者的问题是: 我正在写一个记录飞镖游戏的程序。用户输入球员及其各自的分数。可以查询一名球员的得分,并向程序询问所有球员之间的最佳总得分。我有以下职能: 加分 返回球员得分 返回最佳成绩 退出程序 主要 在main()中,我们首先创建一个新的空字典(比如,players={})。然后,我们要求用户输入一个数字,将他/她带到所选函数(1:添加分数等) 现在,一旦我们进入add_score并添加了一个key:value对(player:score),我们就需要返回到输入选择函数的数字。我只是通过将
谢谢。如果您有一个循环可以执行以下操作 示例:
while True:
players = {}
some code adding to players
此循环将始终将玩家重置为{}
但是,如果您这样做:
players = {}
while something:
some code adding to players
然后,在循环的每个迭代开始时,players
不会被重置
但是如果你有这样的问题,你的问题就不清楚了:
def add_score(dicccionary):
#do something with diccionary
main()
def main():
dicccionary = {}
while something:
option = input("option")
if option == 1:
addscore(dicccionary)
else:
#otherfunction
main()
您的重置问题可以通过以下方式解决:
dicccionary = {} #global variable
def add_score():
#do something with diccionary
main()
def main():
option = input("option")
if option == 1:
addscore()
else:
#otherfunction
main()
顺便说一句,你不应该这样做,试试以下方法:
dicccionary = {} #global variable
def add_score():
#do something with diccionary
def main():
while somecondition:
option = input("option")
if option == 1:
addscore()
else:
#otherfunction
main()
与其从其他每个函数调用
main()
,您只需返回
(或者运行函数的末尾,这相当于返回None
)。由于需要使用main
函数反复运行,因此应该使用循环
def main():
players = {}
while True: # loop forever (until a break)
choice = input("what do you want to do (1-4)")
if choice == "1":
add_score(players)
elif choice == "2":
return_players_score(players)
#...
elif choice == "4":
break # break out of the loop to quit
else:
print("I didn't understand that.")
如果我真的这么做,我会选择这样的方式:
import sys
class ScoreKeeper(object):
def init(self):
self.scores = {}
def add_score(self, player, score):
self.scores[player] = score
def _print_player_score(self, player, score):
print 'player:', player, 'score:', score
def print_scores(self):
for player, score in self.scores.items():
self._print_player_score(player, score)
def best_score(self):
best, player = 0, "no player"
for player, score in self.scores.items():
if score > best:
best, player = score, player
self._print_player_score(player, best)
if __name__ == '__main__':
scorer = ScoreKeeper()
quit = lambda: sys.exit()
choices = quit, scorer.add_score, scorer.print_scores, scorer.best_score
def help():
print 'Enter choice:'
for index, c in enumerate(choices):
print '%d) %s' % (index, c.__name__)
def get_integer(prompt):
res = raw_input(prompt)
try:
return int(res)
except:
print 'an integer is required'
return get_integer(prompt)
def get_choice():
choice = get_integer('choice? ')
if not 0 <= choice < len(choices):
help()
return get_input()
return choice
help()
choice = get_choice()
while(choice):
args = []
if choices[choice] == scorer.add_score:
args.append(raw_input('player name? '))
args.append(get_integer('score? '))
choices[choice](*args)
choice = get_choice()
quit()
导入系统
班级记分员(对象):
def初始化(自):
self.scores={}
def添加分数(自我、玩家、分数):
自我得分[球员]=得分
定义打印玩家分数(自我、玩家、分数):
打印“玩家:”,玩家,“‘分数:”,分数
def打印分数(自我):
对于玩家,在self.scores.items()中得分:
self.\u打印\u玩家\u分数(玩家,分数)
def最佳_分数(自我):
最佳,玩家=0,“无玩家”
对于玩家,在self.scores.items()中得分:
如果得分>最佳:
最佳球员=得分,球员
自我。打印球员得分(球员,最佳)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
记分员=记分员()
quit=lambda:sys.exit()
选择=退出,scorer.add\u分数,scorer.print\u分数,scorer.best\u分数
def help():
打印“输入选项:”
对于索引,枚举中的c(选项):
打印“%d)%s%”(索引,c.\u名称\u)
def get_整数(提示):
res=原始输入(提示)
尝试:
返回整数(res)
除:
打印“需要一个整数”
返回get_整数(提示)
def get_choice():
choice=get_integer('choice?'))
如果不是0,即使它不是英语,是否有必要给出一个好的答案(或者,至少是其中的一部分)也许你应该在循环之外定义字典,只要有一个涉及。否则,如果这里没有代码,你就只能靠自己了。祝贺所有为他们执行OPs任务的人。乐高S,这里有什么问题?正如这里的一些反应所证明的那样,我明确地把它说得不够含糊;还得自己做这项工作。作业期间不允许要求帮助吗?我不是第一个,也不会是最后一个碰到墙需要指导的人,不管是小的还是更具体的。虽然我不能完全确定我是否理解你,但你似乎已经理解我了。这似乎很好,我得试试你的方法,谢谢。python输入函数相当于eval(原始输入(..),所以输入1会使整数1不是“1”,除非你想让用户输入引号。@SamanthaAtkins:在python 3中不是。在Python3中,
input()
相当于Python2的raw\u input()
(它总是返回一个字符串)。谢谢,但不幸的是,这不是我目前的技能。:)老实说,在我看来都不像蟒蛇。。。