I'm programming a dice game. This segment of code is for the input, to make sure I have a correct input (example, 5+4*3/2-1) and not crash the program later.
I'm using easygui for GUI. At first the code was like this:
#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)
It worked fine, and my program wouldn't crash because the input wouldn't be wrong.
However I'm supposed to put the function that calculates the input inside the while-loop to be able to give the user their calculated expression in the messagebox in real time. So I changed the program to this:
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)
If I give a correct input the first time everything is fine but my problem now is that everytime I give the wrong input (2 operators or 2 values next to eachother, start with an operator etc) it says I'm wrong and lets me give another input. This time however the buttons isnt working properly (for example I press del and it adds "del" to my input)
I would really appreciate someone giving me help on this one! Thanks!