我想得到他们的信用评级,如AAA,A,BBB等用户输入,然后分配一个利率给这个。例如,如果用户有一个良好的信用评级,如AAA,我会收取1%的利率
我已经插入了我在VBA中为这个特定函数使用的代码,这样你就知道我想要什么/它是如何工作的,尽管我删除了许多行,因为我添加代码只是为了更好地显示我正在尝试做的事情
creditRate = InputBox("Please enter credit rating:")
If creditRate = "AAA" Then GoTo intcalc Else
If creditRate = "A" Then GoTo intcalc Else
If creditRate = "BBB" Then GoTo intcalc Else
If creditRate = "BB" Then GoTo intcalc Else
If creditRate = "CCC" Then GoTo intcalc Else
If creditRate = "DDD" Then GoTo intcalc Else
If creditRate = "AAA" Then intRate = 0.01 Else
If creditRate = "A" Then intRate = 0.03 Else
If creditRate = "BBB" Then intRate = 0.05 Else
If creditRate = "BB" Then intRate = 0.06 Else
If creditRate = "CCC" Then intRate = 0.08 Else
If creditRate = "DDD" Then intRate = 0.1 Else
在Python中,这很可能是使用dict计算的,dict是一种基于散列的数据结构,允许查找(相当)任意键。这样的dict可以如下创建
然后使用
如果从用户输入设置了
credit_rate
,您可能需要检查它是否有效。你可以用它来做如果要向用户请求有效的输入,请从无效值开始,然后迭代,直到用户提供了有效的输入。一个简单的方法就是
如果您想提供错误消息,那么在可接受值上带有
break
的无限循环可能更具可读性使用python2的读者应该注意使用
raw_input
内置函数,因为在旧版本中input
尝试将输入作为Python表达式进行求值Python还提供了一个默认字典,如果字典中不存在键,它将返回一个默认值
所以呢
以及
相关问题 更多 >
编程相关推荐