python学习第五天

Car game

#start_times=0
#stop_times=0
started=False
while(1):
    command=input('>').lower()
    if('quit'==command):
        break
    elif('help'==command):
        #print("start-to start the car")
        #print("stop-to stop the car")
        #print("quit-to exit")
        print("""
start-to start the car
stop-to stop the car
quit-to exit
""")
    elif('start'==command):
        '''start_times+=1
        if(1==start_times):
            print("car started...Ready to go!")
        else:
            print("Hey,the car is already started,what are you doing!")'''
        if started:
            print("Hey,the car is already started,what are you doing!")
        else:
            started=True
            print("car started...Ready to go!")
    elif ('stop' == command):
        '''stop_times += 1
        if (1 == stop_times):
            print("car stopped")
        else:
            print("Hey,the car is already stopped,what are you doing!")'''
        if not started:
            print("Hey,the car is already stopped,what are you doing!")
        else:
            started=False
            print("car stopped")
    else:
        print("I don't understand that...")

Note

①当类似于不想重复start/stop时,可以通过定义布尔变量的方式,这种方式要比#掉的那种方法占用更少的内存,且代码更加紧凑简洁
②当想要连续输出多行信息时,不必写多个print实现,可以使用"""""",注意这种方式输出的内容和写的格式完全一致:

print("""
start-to start the car
stop-to stop the car
quit-to exit
""")
print("over!")

在这里插入图片描述

print("""
    start-to start the car
    stop-to stop the car
    quit-to exit
""")
print("over!")

在这里插入图片描述
发现"""在结果里面的体现是间距:

print(
    """start-to start the car
    stop-to stop the car
    quit-to exit"""
)
print("over!")

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值