while循环

 

1 # 重复的代码越多,后期维护的成本越高
2 # while循环
3 
4 i = 1
5 while i <= 100:
6     print(i)
7     i += 1
import random
from pip._vendor.distlib.compat import raw_input

i = 0
while i < 3:    #Python分支if和循环while没有用()表示范围,是用缩进标识的,在它后面的缩进都属于函数范围
    i += 1
    player = raw_input('石头,剪刀,布')
    hand = ('石头', '剪刀', '布')
    a = len(hand)
    print(a)
    b = random.randint(0, a - 1)
    print(b)
    computer = hand[b]
    print(computer)
    if (player == '石头' and computer == '剪刀') or (player == '剪刀' and computer == '布') or (
            player == '布' and computer == '石头'):
        print('你赢了')

    elif player == computer:
        print('不分胜负')

    else:
        print('很抱歉,你输了')

 

上一篇:设计模式


下一篇:Java石头剪刀布游戏控制台版(RockPaperScissorsConsole)