|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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?
最开始的时候started为假,在if语句判断那里会跳到else语句里,而else语句会改started为True,这时再输入start,if判断started是True,所以会打印car is already started
|
|