I suggest to use a mapping of operator strings to corresponding functions:
operator_mapping = {
"+": lambda x, y: x + y,
"*": lambda x, y: x * y,
"/": lambda x, y: x / y,
"-": lambda x, y: x - y
}
之后,你可以利用:
operator_function = operator_mapping[operator_choice]
• 尽可能:
question = integerbox("What is " + str(number1) + " " + operator_choice + " " + str(number2) + " ?")
# Calculate the expected answer
expected_answer = operator_function(number1, number2)
# Check if the user s answer is correct
if question == expected_answer:
print("Correct!")
else:
print("Incorrect. The answer is", expected_answer)
遵循你想要达到的完全法典:
from random import randint
from easygui import buttonbox, integerbox
number1 = randint(1, 10)
number2 = randint(1, 10)
operator_choice = buttonbox("What do you want your questions to be?", choices=["+", "*", "/", "-"])
# Mapping operator strings to corresponding functions
operator_mapping = {
"+": lambda x, y: x + y,
"*": lambda x, y: x * y,
"/": lambda x, y: x / y,
"-": lambda x, y: x - y
}
# Get the function corresponding to the selected operator
operator_function = operator_mapping[operator_choice]
# Construct the question
question = integerbox("What is " + str(number1) + " " + operator_choice + " " + str(number2) + " ?")
# Calculate the expected answer
expected_answer = operator_function(number1, number2)
# Check if the user s answer is correct
if question == expected_answer:
print("Correct!")
buttonbox("Correct")
else:
print("Incorrect. The answer is", expected_answer)
buttonbox("Try next one ...")
请注意,按照本法典的规定,我有点使用了从愤怒到扼杀的转换,然后又回到愤怒状态。
为使《守则》成为从1到10号基本业务的培训员,这一版本使得有可能在不需要重新启动的情况下继续实施,允许在不正确的情况下作出多重答复,并解决在结果不是仓促价值或负面的情况下,就划分和减员提出问题:
from random import randint
from easygui import buttonbox, integerbox, textbox
msgBoxSampleText = "This is a sample text.
You can display multiple lines using the newline character."
msgBoxWindowTitle = "Sample Text Input Box"
#print ( textbox(msgBoxSampleText, msgBoxWindowTitle) )
#exit()
alternative_to_operator_mapping_using_lambda ="""
import operator
operator_mapping = {
+ : operator.add,
- : operator.sub,
* : operator.mul,
/ : operator.truediv,
% : operator.mod,
** : operator.pow,
// : operator.itruediv
}"""
# Mapping operator strings to corresponding functions
operator_mapping = {
"+": lambda x, y: x + y,
"*": lambda x, y: x * y,
"/": lambda x, y: x / y,
"-": lambda x, y: x - y
}
response = "Next one please ..."
while response == "Next one please ..." :
operator_choice = buttonbox("What do you want your questions to be?", choices=["+", "*", "/", "-"], default_choice="+")
if operator_choice is None :
break
# Get the function corresponding to the selected operator
operator_function = operator_mapping[operator_choice]
modulo = 1
while modulo != 0 :
number1 = randint(1, 10)
number2 = randint(1, 10)
if operator_choice == "/":
modulo = number1 % number2
elif operator_choice == "-" :
if number2 > number1:
modulo = 1
else:
modulo = 0
else :
modulo = 0
# Construct the question
question = integerbox("What is " + str(number1) + " " + operator_choice + " " + str(number2) + " ?")
# Calculate the expected answer
expected_answer = operator_function(number1, number2)
if question == expected_answer :
print("Correct!")
print( f"{response=}" )
else:
while question != expected_answer :
print("Incorrect. The answer is", int(expected_answer))
buttonbox("Try once again ...", choices=["OK"], default_choice="OK")
question = integerbox("What is " + str(number1) + " " + operator_choice + " " + str(number2) + " ?")
response = buttonbox("Correct", choices=["OK", "Next one please ..."], default_choice="Next one please ..." )