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!")