楼主: 风晚凉
9528 4

[问答] python while True 的用法和意思 [推广有奖]

  • 0关注
  • 1粉丝

大专生

85%

还不是VIP/贵宾

-

威望
0
论坛币
1532 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
297 点
帖子
25
精华
0
在线时间
93 小时
注册时间
2012-3-11
最后登录
2020-11-11

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
# Use bisection search until the search space is sufficiently small
while True:
    balance = original_balance
    monthly_payment = (low_payment + high_payment)/2

    # Simulate passage of time until outstanding balance is paid off
    # Each iteration represents 1 month
    for month in range(1,13):
        interest = round(balance*interest_rate/12, 2)
        balance += interest - monthly_payment
        if balance <= 0:
            break

    if (high_payment - low_payment < 0.005):
        # Bisection search space is small enough
        # Print result
        print "RESULT"

        # Round monthly payment up to the nearest cent
        monthly_payment = round(monthly_payment + 0.004999, 2)
        print "Monthly payment to pay off debt in 1 year:", round(monthly_payment,2)

        # Recompute remaining balance and the number of months needed
        balance = original_balance
        for month in range(1,13):
            interest = round(balance*interest_rate/12, 2)
            balance += interest - monthly_payment
            if balance <= 0:
                break
        print "Number of months needed:", month
        print "Balance:", round(balance,2)
        break
    elif balance < 0:
        #Paying too much
        high_payment = monthly_payment
    else:
        #Paying too little
        low_payment = monthly_payment

这长串代码是用二分法找break-even的点。具体内容不用管。刚开始学python,所以有点搞不明白while true这个条件语句到底是什么意思。如果True 的意思是不等于0,也是说得通的,就是balance不等于0的话就一直run。但他只写while true 没有指明是对哪个变量,所以是全局变量还是什么?

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python while Rue Hil TRU represents interest balance search python

回帖推荐

leewg 发表于2楼  查看完整内容

True是布尔值, while True : 就是一直执行循环, 死循环
沙发
leewg 发表于 2016-11-23 16:33:04 |只看作者 |坛友微信交流群
True是布尔值, while True : 就是一直执行循环, 死循环

使用道具

藤椅
prince315 在职认证  发表于 2016-11-24 17:44:37 |只看作者 |坛友微信交流群
具体内容不谈,应该是一个整体的测试阶段,这样设定为True就可以跑这一段逻辑来看结果,如果后续这一段不需要跑了,将True改为False即可。

使用道具

板凳
hanhyojoo1992 发表于 2017-7-20 15:11:38 |只看作者 |坛友微信交流群
while true 就是你不中断 就一直循环

使用道具

报纸
w3746 发表于 2017-7-25 19:24:33 |只看作者 |坛友微信交流群
while True意思是要一直进行死循环,一般while true 循环中会加入break条件判断用以在循环内部的某个条件达成时终止循环。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-7 19:09