首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >这个if语句和打印的问题在哪里?

这个if语句和打印的问题在哪里?
EN

Stack Overflow用户
提问于 2020-10-03 16:23:56
回答 2查看 48关注 0票数 0

我正在努力理解我正在做的这个测试练习。我编写了下面的代码并执行它,但是结果并不是我真正想要的。第一个if语句应该打印日期或天数,具体取决于所传递的天数。最后的打印,解决方案,应该打印的日子加上文字之间的空格,然而,我不能做到这一点。提前感谢!

代码语言:javascript
运行
复制
well_height = 125
daily_distance = 30
nightly_distance = 20
snail_position = 0
notEscaped = True

# Create a variable days to keep
# count of the days that pass until the snail escapes the well

days = 0

fullDaylydist = daily_distance - nightly_distance

while snail_position < well_height:
    if days <= 1:
        print(str(days) + " day gone")
    else:
        print(str(days) + " days gone")
    snail_position += fullDaylydist
    if snail_position < well_height:
        days += 1
# Print the solution.
    else:
        print(str(days) + " days and I am out from the well!")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-03 17:06:45

这里的诀窍是蜗牛白天向前移动,晚上向后移动。蜗牛在睡觉前一天会到达顶端。

代码语言:javascript
运行
复制
well_height = 125
daily_distance = 30
nightly_distance = 20
snail_position = 0
notEscaped = True

days = 1
snail_position = 0

fullDaylydist = daily_distance - nightly_distance  # assumes full day

while snail_position < well_height - daily_distance:  # check if snail reaches top during day
    if days <= 1:
        print(str(days) + " day gone")
    else:
        print(str(days) + " days gone")
    snail_position += fullDaylydist
    if snail_position < well_height:
        days += 1
# Loop done, Print the solution.
print(str(days) + " days and I am out from the well!")  # last day

输出

代码语言:javascript
运行
复制
1 day gone
2 days gone
3 days gone
4 days gone
5 days gone
6 days gone
7 days gone
8 days gone
9 days gone
10 days gone
11 days and I am out from the well!
票数 0
EN

Stack Overflow用户

发布于 2020-10-03 16:42:53

第一个if小于或等于一天。您的意思是这个,还是应该说大于或等于>=

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

https://stackoverflow.com/questions/64186446

复制
相关文章

相似问题

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