python3--str的使用1

#字符串
sentence = 'I am an English sentence'
#split默認空格分割字符串,還可以使用換行符\n、製表符\t分割字符串
print (sentence.split())
#split 指定字符串分隔符
section = 'Hi.I am the one.Bye.'
print(section.split('.'))

##罰球遊戲以3分定勝負



from random import choice
dircetion= ['L','M','R']
score = [0,0]#computer,person
rounds = 3
flag=False
#首先得到3分者獲勝
#定義函數,模擬得分過程
def kick():
    you = input('choose you side to shoot:')
    #隨機取序列中的一個值
    com = choice(dircetion)
    #com = 'M'
    print('your choose is %s,computer‘s choose is %s' %(you.upper(),com))
    #使輸入大寫
    if you.upper() == com:
        score[0]+=1
    else:
        score[1]+=1
    print('Score is %d(computer) - %d(you)' %(score[0],score[1]))  


while(flag == False):
   # print('flag=' + flag)
    for i in range(rounds):
        print('==========Round%d===========' %(i+1))
        kick()
        #比較值大小‘==’
        if score[0] == 3 :
            print('computer is the winner!')
            flag=True
            #跳出for循環,break
            break
        elif score[1] == 3:
            print('you is the winner!')
            flag=True
            break
        else:
            rounds+=1
            i+=1        
            print('go on...')
            
    
    

    
    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章