Course notes_zero-based introductory learning Python_004_improve our little game

Improve our mini game

  • Condition comparison

    >>> 1 < 3
    True
    >>> 1 > 3
    False
    >>> 1 == 1
    True
    >>> 1 == 2
    False
    >>> 1 != 1
    False
    
  • Conditional branch syntax

    Pay attention to the colon and indentation, there is no {}

    temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
    guess = int(temp)
    if guess == 8:
        print("我草,你是小甲鱼心里的蛔虫嘛?")
        print("哼,猜对了也没有奖励")
    else:
    	if guess > 8: #if-else语句,注意缩进和冒号
    		print("哥,大了大了")
    	else:
    		print("嘿,小了,小了")
    print("游戏结束,不玩啦")
    
  • loop statement

    temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
    guess = int(temp)
    i=0
    while guess != 8 and i < 3: #while循环语句
        i = i + 1
        temp = input("哎呀,猜错了,请重新输入吧:")
        guess = int(temp)
        if guess == 8:
            print("我草,你是小甲鱼心里的蛔虫嘛?")
            print("哼,猜对了也没有奖励")
        else:
    		if guess > 8: #if-else语句,注意缩进和冒号
    			print("哥,大了大了")
    		else:
    			print("嘿,小了,小了")
    print("游戏结束,不玩啦~~")
    
  • random function

    import random #导入random函数库
    secret = random.randint(1,5) #生成1—5之间的整数
    temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
    guess = int(temp)
    i=0
    while guess != secret and i < 3:
        i = i + 1
        temp = input("哎呀,猜错了,请重新输入吧:")
        guess = int(temp)
        if guess == secret:
            print("我草,你是小甲鱼心里的蛔虫嘛?")
            print("哼,猜对了也没有奖励")
        else:
            if guess > secret: #if-else语句,注意缩进和冒号
                print("哥,大了大了")
            else:
                print("嘿,小了,小了")
    print("游戏结束,不玩啦~~")
    

Guess you like

Origin blog.csdn.net/weixin_41754258/article/details/113919576