python - python,while循环,函数内部调用的函数
问题描述
我对编程相当陌生,我正在使用 cs50IDE 编写一些进行克拉和重量计算的代码。该bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000)
功能似乎无法正常工作。当我运行以下代码并选择"yes"
at"Would you like to bring the carats up to {target_ct}ct/{target_per1000}? "
而不是打印最后一个输出时,光标在控制台中保持活动状态,如果我退出程序,则会出现此消息:
`Traceback (most recent call last):
File "carats.py", line 65, in <module>
main()
File "carats.py", line 29, in main
to_add = bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000)
File "carats.py", line 61, in bring_up
result = calc_ct(_9ct, _14ct, new_18ct, _22ct)
File "carats.py", line 39, in calc_ct
result = int((a+b+c+d)/tot_weight)
KeyboardInterrupt`
我试图在函数的while循环中包含print(new_18ct)
和,它似乎卡在循环中。我试图用一个结构替换while循环,结果是它打印了main函数的最后一句,如下所示:print(result)
bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000)
if...else
You need to add **None**gr of {target_ct}ct
希望大家能帮帮我,谢谢!!!
`
# work out resulting carat from mixed carats
from cs50 import get_float, get_string
def main():
# prompt user for weight of different carats:
print("Insert weight in grams of customers gold in the following carats:")
_9ct = get_float("9ct: ")
_14ct = get_float("14ct: ")
_18ct = get_float("18ct: ")
_22ct = get_float("22ct: ")
#calculate resulting carats and weight after loss in casting
result = calc_ct(_9ct, _14ct, _18ct, _22ct)
minusloss = format(((_9ct + _14ct + _18ct + _22ct) * 0.95), '.2f')
print()
print(f"Estimated resulting carats: {result}ct")
print()
# check if user wants to achieve specific carat weight
target_ct, target_per1000 = target(result)
check = get_string(f"Would you like to bring the carats up to {target_ct}ct/{target_per1000}? ")
if check.lower() in ["n", "no"]:
print(f"Remember to consider a 5% loss: total weight({(_9ct + _14ct + _18ct + _22ct)}gr) -5% = {minusloss}gr")
print("See you next time!")
elif check.lower() in ["y", "yes"]:
# calculate how much metal of each carat they need to add to
to_add = bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000)
print(f"You need to add {to_add}gr of {target_ct}ct")
def calc_ct(_9ct, _14ct, _18ct, _22ct):
a = _9ct * 375
b = _14ct * 585
c = _18ct * 750
d = _22ct * 916
tot_weight = _9ct + _14ct + _18ct + _22ct
result = int((a+b+c+d)/tot_weight)
return result
def target(result):
target_ct = 0
target_per1000 = 0
if result > 375 and result < 585:
target_ct = 14
target_per1000 = 585
if result > 585 and result < 750:
target_ct = 18
target_per1000 = 750
if result > 750 and result < 916:
target_ct = 22
target_per1000 = 916
return target_ct, target_per1000
def bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000):
to_add = 0
result = calc_ct(_9ct, _14ct, _18ct, _22ct)
while result < target_per1000:
new_18ct = _18ct + 0.5
result = calc_ct(_9ct, _14ct, new_18ct, _22ct)
to_add += 0.5
return to_add
main()
```
----------
解决方案
看起来,在你的循环中,你只是每次都为你的变量结果赋予一个新值,而不是加起来。尝试通过简单地将归属符号 ('=') 更改为增量 ('+=') 来将前一个值相加
def bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000):
to_add = 0
result = calc_ct(_9ct, _14ct, _18ct, _22ct)
while result < target_per1000:
new_18ct = _18ct + 0.5
result += calc_ct(_9ct, _14ct, new_18ct, _22ct)
to_add += 0.5
return to_add
欢迎编程 :)
推荐阅读
- java - 即使在使用 HttpStatus.BAD_REQUEST 返回 ResponseEntity 之后,在 API 响应中获取 HTTP 响应代码 200
- c - 如何使用 libgit2 创建一个空提交?
- android - 如何使用 SugarRecord 和 Kotlin 查询多个子句参数
- python - 在给定其他项目的情况下计算列表中出现的项目
- azure - Flask Dance OAuth 通过 Microsoft Azure 错误
- amazon-web-services - 创建 cloudformation 模板时预填充键(但不是值)标签
- javascript - 如何用 React Link 组件替换部分字符串
- android - 在 contraintlayout 中获取子布局的高度和宽度
- java - 从可执行 jar 运行 spring 测试
- wso2 - WSO2 开发人员工作室不显示 API 资源属性选项卡