我正在编写一个骰子游戏。这段代码用于输入,以确保我有一个正确的输入(例如,5 + 4 * 3 / 2-1),以后不会崩溃程序。
我正在使用easygui进行GUI。起初代码是这样的:
#possible_inputs = a list of operators (+-*/) and the values from the dices (1-6) as well as "OK, "Del" and "Reroll"
#raw_user_input = empty list
#br_value - the target value
#dice_value_list - the values from the dices
var = 0
while(var != possible_inputs.index("OK")):
var = eg.indexbox(raw_user_input, "Targetvalue: " + br_value_str, possible_inputs)
if(var != possible_inputs.index("OK")):
raw_user_input.append(possible_inputs[var])
if(var == possible_inputs.index("Del")):
raw_user_input[:] = []
if(var == possible_inputs.index("Reroll")):
#reroll dices
if(len(raw_user_input) == 0):
eg.msgbox("No input given", "ERROR")
return gui_input(dice_value_list, br_value)
for r in range(len(raw_user_input)):
if(r%2 == 0):
if(raw_user_input[r] not in dice_value_list):
eg.msgbox("Incorrect expression", "ERROR")
return gui_input(dice_value_list, br_value)
else:
if(raw_user_input[r] not in allowed_operators):
eg.msgbox("Incorrect expression", "ERROR")
return gui_input(dice_value_list, br_value)
它工作正常,我的程序不会崩溃,因为输入不会出错。
但是我应该在while循环中放置计算输入的函数,以便能够实时向用户提供他们在消息框中的计算表达式。 所以我把程序改为:
input_value_calc = 0
while(var != possible_inputs.index("OK")):
var = eg.indexbox(raw_user_input, "Target value: " + br_value_str + ", current target value " + str(input_value_calc), possible_inputs)
if(var != possible_inputs.index("OK")):
raw_user_input.append(possible_inputs[var])
if(var == possible_inputs.index("Del")):
raw_user_input[:] = []
if(var == possible_inputs.index("Reroll")):
#reroll dices
br_check, values_input, input_value_calc = user_input_value_check(raw_user_input,br_value):
def user_input_value_check(raw_user_input,br_value):
if(len(raw_user_input) == 0):
eg.msgbox("No input given", "ERROR")
gui_input(dice_value_list, br_value)
for r in range(len(raw_user_input)):
if(r%2 == 0):
if(raw_user_input[r] not in dice_value_list):
eg.msgbox("Incorrect expression", "ERROR")
gui_input(dice_value_list, br_value)
else:
if(raw_user_input[r] not in allowed_operators):
eg.msgbox("Incorrect expression", "ERROR")
gui_input(dice_value_list, br_value)
#rest of the program calculates the score, which works fine
#the function returns True/False (if the input evaluates to the correct br_value), values_input (again for other functions in the program), input_value_calc (the evaluated input value)
如果我第一次给出正确的输入一切都很好但我现在的问题是每次我给出错误的输入(2个操作员或2个值彼此相邻,从操作员开始等)它说我错了让我给另一个输入。但是这次按钮无法正常工作(例如我按del并将“del”添加到我的输入中)
我真的很感谢有人给我这个帮助!谢谢!
答案 0 :(得分:0)
@cataclysmic,
也许还有另一种方法可以实现你的目标。例如,您在用户完全输入信息后进行数据验证。相反,为什么不强迫用户做正确的事情。因此,不应该允许用户先按'*'。首先应该要求他们按压模具。然后他们应该被迫只选择一个操作符(比如'*'),然后在选择die和选择操作之间交替(如在时钟滴答中... tock ... tick ... tock)。
此外,一旦用户选择了骰子,他们就不应该再次选择它(我假设)。因此,选择后将其从列表中删除。
请考虑以下代码。它并不完美,但我试图让它与你想要做的类似。我已经在一个比你更新的easygui版本上测试了这个,所以我不能保证它有效:
import easygui as eg
def get_user_input(target_value, dice_rolls):
operator_choices = ['+', '-', '*', '/']
operator_choices.extend(['OK', 'Del', 'Reroll'])
dice_choices = [str(r) for r in dice_rolls]
dice_choices.extend(['Del', 'Reroll'])
raw_user_input = list()
mode = 'tick'
while True:
if mode == 'tick':
choices = dice_choices
else:
choices = operator_choices
var = eg.indexbox(''.join(raw_user_input), "Target value: {}".format(target_value), choices)
if var is None:
raise ValueError("Dialog closed with invalid entry")
choice = choices[var]
if choice == 'OK':
return ''.join(raw_user_input)
if choice == 'Del':
raw_user_input = list()
dice_choices = [str(r) for r in dice_rolls]
dice_choices.extend(['Del', 'Reroll'])
mode = 'tick'
continue
if choice == 'Reroll':
return None
raw_user_input.append(choice)
if mode == 'tick': # Remove the dice from the list of dice
del dice_choices[dice_choices.index(choices[var])]
if mode == 'tick':
mode = 'tock'
else:
mode = 'tick'
br_value = 12 # Sample value
dice_value_list = [4, 2, 1, 1, 5] # Sample rolls
try:
user_input = get_user_input(br_value, dice_value_list)
except:
print("Your data entry was cancelled by the user")
exit()
if user_input is None:
print("Rerolling the dice")
#.... do that now!
print("User entered: {}".format(user_input))
####
# Now, put code here to process your user_input
# ....
#
####
请继续编程!它有时很难,但也有回报!