使用从用户输入参数中选择的数字执行操作

2024-04-26 01:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我基本上是在尝试用Python制作一个数据库,其中每个“数据”将由一个字符串和一个等价的数字(或接近它的某个数字)组成,我正在寻找一种方法,允许最终用户输入与数据库中某个字符串相对应的输入,并将其引用到相应的float值,然后执行简单的算术运算。 这是我做的,它什么都不做

c = float(2.66)
d = float(6.68)
test = input('Enter the letter')
test2 = input('Enter the second letter')
if test == c and test2 == d:
    print(float(c)+float(d))

编辑: 我只是重新访问了这一页,找到了解决问题的方法,只缺少代码中的引号和变量定义。正确的方法如下:

c = float(2.66)
d = float(6.68)
test = str(input('Enter the letter'))
test2 = str(input('Enter the second letter'))
if test == str('c') and test2 == str('d') :
    print(float(c)+float(d))

如果这个需要删除我会做的


Tags: andthe方法字符串test数据库inputif