鱼C论坛

 找回密码
 立即注册
查看: 2824|回复: 5

[已解决]求大神来解答一下!

[复制链接]
发表于 2020-2-19 16:42:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 soreverything 于 2020-2-19 16:48 编辑

command = ''
started = False
while True:
    command = input(">").lower()
    if command == 'start':
        if started:
            print("car is already started!")
        else:
            started = True
            print("car started!")
    elif command == 'stop':
        if not started:
            print("car is already stopped!")
        else:
            started = False
            print("car stopped!")
    elif command == 'help':
        print('''
start - start the car
stop - stop the car
quit - exit
        ''')
    elif command == 'quit':
        break
    else:
        print("I don't understand that")  

这个代码是正确的,我的问题是:开始程序定义了一个started = false,然后while循环里面有个 if started:,程序运行的时候我第一次输入start 结果是:car stated! 然后我再次输入start,为什么是 car is already started?,为什么我第一次输入start,结果不是car is already start?
最佳答案
2020-2-19 16:57:41
soreverything 发表于 2020-2-19 16:46
哥,我修改了一下,你看看

最开始的时候started为假,在if语句判断那里会跳到else语句里,而else语句会改started为True,这时再输入start,if判断started是True,所以会打印car is already started
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-19 16:44:32 From FishC Mobile | 显示全部楼层
你想问什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-19 16:46:00 | 显示全部楼层

哥,我修改了一下,你看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-19 16:48:50 From FishC Mobile | 显示全部楼层
soreverything 发表于 2020-2-19 16:46
哥,我修改了一下,你看看

初始为假,所以执行else  后面的语句,同时改变为真,再次进入执行else前面的语句
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-19 16:57:41 | 显示全部楼层    本楼为最佳答案   
soreverything 发表于 2020-2-19 16:46
哥,我修改了一下,你看看

最开始的时候started为假,在if语句判断那里会跳到else语句里,而else语句会改started为True,这时再输入start,if判断started是True,所以会打印car is already started
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-19 17:04:40 | 显示全部楼层
wp231957 发表于 2020-2-19 16:48
初始为假,所以执行else  后面的语句,同时改变为真,再次进入执行else前面的语句

谢谢哥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-2 16:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表