本文實例為大家分享了python點球小游戲的具體代碼,供大家參考,具體內容如下
1.游戲要求:
設置球的方向:左中右三個方向,射門或者撲救動作,循環5次,直接輸入方向。電腦隨機挑選方向,如果方向相同,那么電腦得分,如果方向相反,那么人得分。
2.分析如何寫程序:
1)循環,使用for ..in range()
2) if ..else
3)from random import choice 隨機選擇
3.腳本如下:
from random import choicescore_person=0score_com=0location=['left','center','right']for i in range (5): print ("----Round %d You kicked----"%(i+1)) com_choice=choice(location) print ("Computer's choice is %s"%com_choice) print ("input what your choice:left/center/right") you_choice=input() print ("You have choose:"+you_choice) if you_choice!=com_choice: # 方向不同,球進! score_person+=1 #人得分 print ("Kicked!") else: print ("Saved unsuccesfully!") #補救 print ("Score:%d(person)-%d(com)/n" %(score_person, score_com)) print ("----Round %d You saved----"%(i+1)) com_choice=choice(location) print ("Computer's choice is %s"%com_choice) print ("input what your choice is:left/center/right") if you_choice==com_choice: #方向相同,球不進! print ("Saved unsucessfully!") score_com+=1 #電腦得分 else: print ("Kicked") print ("Score:%d(person)-%d(com)/n"%(score_person, score_com))這小游戲的功能類似于猜數游戲。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答