开始跟着视频学python,第六天mark【while if else】

视频位置:80:55
开始讲循环了。感觉大学c语言学的还剩数组了吧。
还记得有的题是循环里面让变量减1,然后让填会运行多少次。。。。。。

i = 8
while i <= 10:
    print(i)
    i += 1
print("bye")

输出:

8
9
10
bye

一个小花样:

i = 1
while i <= 5:
    print('*' * i)   #   *重复i次
    i += 1
print("bye")

输出:

*
**
***
****
*****
bye

一个猜数游戏:
请猜一猜这个数是几,你有三次机会哦!猜对有奖哦!
这是我写的:

i = 5   #答案
n = 3    #猜的次数
a = int(input("请输入0-9中任意一个数: "))
while n > 0:
    if a == i:
        print(f"恭喜你猜对啦!这个数就是{i}!")
        break
    else:
        n = n - 1
        if n == 0:
            print(f"机会用尽了,这是数是{i}")
            break
        a = int(input("猜错了,再猜一次吧!  "))
print("bye")

在pycharm里有个小功能,选中一个变量,如i,右键,refactor—rename,此功能可以批量重命名我们的变量。
视频主这样写的:

secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
    guess = int(input("Gusess: "))
    guess_count += 1
    if guess_count == secret_number:
        print("you win")
        break
else:      #   while的else
    print("sorry you fail")

下面,我们来做一个游戏,虽然没有界面…但这算是游戏的引擎了…视频时间:91:10
小游戏设计如下:
输入help,则输出 start stop quit三个说明
输入其他的则输出: 你说什么?我看不懂
输入start ,输出:系好安全带,准备出发
输入stop ,输出:车一下子就停住了。
输入 quit,退出游戏
下面是我的代码:

i = 1
while i !=0:
    commd = input()
    if commd == 'help':
        print("start    car start"
              "stop     car stop"
              "quit     quit game")    #此处输出并没有换行
    elif commd == 'start':
        print("car start")
    elif commd == 'stop':
        print("car sotp")
    elif commd == 'quit':
        print('bye')
        break
    else:
        print("I don't understand what you say")

输出测试:

help
start    car startstop     car stopquit     quit game
stop
car sotp
start
car start
ds
I don't understand what you say
quit
bye

视频主是这么写的:

command = ""
while True:      #也可以写while command != "quit",但和下面的break重复了,故简化。
    command = input(">").lower()   #此处确保输入自动小写化
    if command == "start":
        print("car started ....")
    elif command == "stop":
        print("car sotped ...")
    elif command == "quit":
        break
    elif command == "help":
        print("""
start  - to start the car   #此处的内容随便写,所以前面的空格(缩进)都可以去掉,这样就顶个显示。
stop   - to stop the car
quit   - to quit game
        """)
    else:
        print("sorry i don't understand that")

升级游戏功能:
输入start后再次输入,会提示车已启动
输入stop后再次输入,,会提示车已停止
车未启动就输入stop,会提示车未启动
在视频主的基础上,我的修改如下:

command = ""
car_status = "stop"
while True:      #也可以写while command != "quit",但和下面的break重复了,故简化。
    command = input(">").lower()   #此处确保输入自动小写化
    if command == "start":
        if car_status == "stop":
            print("car started ....")
            car_status = "start"
        else:
            print("car is already started.")

    elif command == "stop":
        if car_status == "start":
            print("car stoped ....")
            car_status = "stop"
        else:
            print("car is already stoped.")
    elif command == "quit":
        break
    elif command == "help":
        print("""
start  - to start the car   
stop   - to stop the car
quit   - to quit game
        """)
    else:
        print("sorry i don't understand that")

我添加了一个车的状态变量,每次输入命令后更新车的状态。

>sta
sorry i don't understand that
>start
car started ....
>start
car is already started.
>stop
car stoped ....
>stop
car is already stoped.
>start
car started ....
>quit

视频主是添加了一个布尔值,started = Fales,思路一致。

视频时间位置102:00,下一节是for循环。今天到这里。

猜你喜欢

转载自blog.csdn.net/weixin_42944682/article/details/105563371