首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python,while循环,函数在函数内部调用

python,while循环,函数在函数内部调用
EN

Stack Overflow用户
提问于 2020-03-04 02:37:03
回答 1查看 44关注 0票数 0

我是编程新手,我正在使用cs50IDE编写一些执行克拉和重量计算的代码。bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000)函数似乎不能正常工作。当我运行以下代码并在"Would you like to bring the carats up to {target_ct}ct/{target_per1000}? "中选择"yes"而不是打印最后一次输出时,光标在控制台中保持活动状态,如果我退出程序,则会出现以下消息:

代码语言:javascript
复制
`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`

我曾尝试在bring_up(_9ct, _14ct, _18ct, _22ct, target_per1000)函数的while循环中包含print(new_18ct)print(result),但它似乎卡在了循环中。我尝试将while循环替换为if...else结构,结果是它输出了main函数的最后一句话,如下所示:

代码语言:javascript
复制
 You need to add **None**gr of {target_ct}ct

希望您能帮助我,谢谢!

代码语言:javascript
复制
`
    # 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()

代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

发布于 2020-03-04 03:18:23

看起来,在你的循环中,你只是每次都给你的变量result赋予一个新的值,而不是加起来。通过简单地将属性符号('=')更改为增量('+='),尝试将前一个值相加

代码语言:javascript
复制
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

欢迎学习编程:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60513595

复制
相关文章

相似问题

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