首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在不同条件下打印一个变量的两个值

在不同条件下打印一个变量的两个值
EN

Stack Overflow用户
提问于 2022-02-09 15:49:40
回答 1查看 34关注 0票数 0

请考虑以下声明:

sum_value = fixed_value - current_value

其中fixed_value是常数,current_valuethresholds的函数;

thresholds有两个threshold_level值:thresholds = [10, 20]

我需要找到一个sim_value的rato,对应于threshold_level = 10sim_value,对应于threshold_level = 20,即final_sim_value = sim_value_at_10/sim_value_at_20

代码部分是

代码语言:javascript
运行
复制
thresholds = [10, 20]
fixed_value = 100

for threshold_level in thresholds:
    current_value = 5 - threshold_level
    sim_value = fixed_value - current_value

    def sim_value_multi(threshold_level):
        if threshold_level == 10:
            sim_value_at_10 = sim_value
            return sim_value_at_10
        if threshold_level == 20:
            sim_value_at_20 = sim_value
            return sim_value_at_20 
            
    final_sim_value = sim_value_multi(10)/sim_value_multi(20)
    
    print('sim_value_multi(10) is ', sim_value_multi(10))
    print('sim_value_multi(20) is ', sim_value_multi(20))
    print('final_sim_value is ', final_sim_value)
    
print('--------------------')
final_sim_value = sim_value_multi(10)/sim_value_multi(20)
    
print('sim_value_multi(10) is ', sim_value_multi(10))
print('sim_value_multi(20) is ', sim_value_multi(20))
print('final_sim_value is ', final_sim_value)

它提供了这样的输出:

代码语言:javascript
运行
复制
sim_value_multi(10) is  105
sim_value_multi(20) is  105
final_sim_value is  1.0
sim_value_multi(10) is  115
sim_value_multi(20) is  115
final_sim_value is  1.0
--------------------
sim_value_multi(10) is  115
sim_value_multi(20) is  115
final_sim_value is  1.0

请你纠正我或提出适当的解决办法好吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-09 16:51:51

你想得到这个结果吗?

代码语言:javascript
运行
复制
thresholds = [10, 20]
fixed_value = 100
current_values = []
for threshold_value in thresholds:
    current_values.append(fixed_value + threshold_value - 5)

print('sim_value_multi(10) is ', current_values[0])
print('sim_value_multi(20) is ', current_values[1])
print('final_sim_value is ', current_values[0]/current_values[1])

输出

代码语言:javascript
运行
复制
sim_value_multi(10) is  105
sim_value_multi(20) is  115
final_sim_value is  0.9130434782608695
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71052668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档