首先,为了提供一些背景,我正在设计一个基于控制台的二十一点版本,用于娱乐和学习目的.下面的代码是我的解决方案,用于判断给定用户输入的不同条件.
下面的函数处理睡眠时间和用户输入,其文件被导入到Main.
import consoleControl as cs
def console_control( string, n_sec, return_flag, break_check, condition, wrapper=str)
如果期望返回,则将RETURN_FLAG设置为True,如果传递了条件,则将执行以下代码.
if condition:
while True:
try:
# This is only meant to be used on numeric types
userIo = wrapper(input(string))
if eval(str(userIo) + condition):
return userIo
else:
return None
except ValueError:
return None
我的第一次成功运行包括以下函数调用.
# Gets a positive numeric value
def get_bankRoll():
while True:
# condition being passed to console_control()
condition = '> 0'
try:
while True:
string = 'How much money will you be gamblin\'... player? \nEuros: '
cash = hud_control(0, None, None, None, None, [string, None, True, None, condition, float])
# more code follows
呼叫至->;
# Printing routine with set ascii charaters and other printed statements
def hud_control(hud, cash, game_phase, hand_display_args, new_line, control_args):
# Code between '#' removed for clarity
if hud == 0:
pass
else:
pass
if new_line:
for x in range(new_line): print('\n')
#
if control_args:
if control_args[-1]:
return cs.console_control(*control_args)
else:
cs.console_control(*control_args)
我已经读到了val是非常不安全的,事实上,当我测试删除包装器并计算出"sys.exit(0)#"时,我成功地退出了.但是使用包装器我就不能了,所以假设我只想计算数字表达式.
以下任何一个对我来说都是很好的答案:
- 人们将如何利用当前的代码?
- 动态(输入、条件)判断的替代方案是什么?
注意:如果可能的话,我更喜欢无类的实现.