【问题标题】:while loop and booleanwhile 循环和布尔值
【发布时间】:2017-11-20 12:11:00
【问题描述】:

不明白如何让循环停止并继续/重复 另外,不了解如何使布尔值的第二部分起作用

monthly_investment = float(input("Enter monthly investment:\t"))
while monthly_investment < 1:
    print("Entry must be greater than 0")

yearly_interest_rate = float(input("Enter yearly interest rate:\t"))
while yearly_interest_rate < 1 and yearly_interest_rate < 16:
    print("Entry must be greater than 0 and less than or equal to 15" )

【问题讨论】:

标签: python while-loop boolean


【解决方案1】:

尼姆科拉,

你只需要在while循环的末尾添加另一个赋值语句,这样一旦用户输入了一个有效的输入,程序就可以跳出while循环。

monthly_investment = float(input("Enter monthly investment:\t"))
while monthly_investment < 1:
    print("Entry must be greater than 0")
    monthly_investment = float(input("Enter monthly investment:\t"))

yearly_interest_rate = float(input("Enter yearly interest rate:\t"))
while yearly_interest_rate < 1 and yearly_interest_rate < 16:
    print("Entry must be greater than 0 and less than or equal to 15" )
    yearly_interest_rate = float(input("Enter yearly interest rate:\t"))

【讨论】:

    猜你喜欢
    • 2011-08-01
    • 2020-12-03
    • 2021-11-09
    • 2015-05-24
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多