我正在參加「創意開發 投稿大賽」詳情請看:掘金創意開發大賽來了!
昨天寫了生命游戲,Can not help but think of just contactpython的時候,I'm still very unfamiliar with all kinds of programming,The first one to do is a game of rock-paper-scissors,正巧Nuggets Creative Development Call for PapersWant to rush a wave of lift tables,Might as well put this up(手動狗頭.jpg).
當然,對於剛剛接觸編程的同學,Rock-paper-scissors is also a good way to help you fully familiarize yourself with conditional control and looping statements.
This game is old and simple,It is full of philosophical thoughts of one thing subduing one thing and mutual generation and mutual restraint~~
總而言之,其規則如下:
In order to complete the game flow smoothly,我們需要做到以下幾點:
總體代碼如下所示:
import random
import os
import re
// 用戶(我們)的選擇
def UserChoiceText(userChoice):
if 'S' == str.upper(userChoice):
return '石頭'
elif 'J' == str.upper(userChoice):
return '剪刀'
elif 'B' == str.upper(userChoice):
return '布'
os.system('cls' if os.name=='nt' else 'clear')
while (True):
print("\n")
print("石頭, 剪刀, 布 - 開始......")
userChoice = input("Make your choice:[S]石頭,[J]剪刀,[B]布,[T]退出: ")
if 'T' == str.upper(userChoice):
print("You have opted out,GAME OVER.")
break
if not re.match("[JjSsBb]", userChoice):
print("Only the letters below can be selected:")
print("[S]石頭, [J]剪刀, 或 [B]布.")
continue
print("你的選擇: " + UserChoiceText(userChoice))
choices = ['S', 'B', 'J']
opponenetChoice = random.choice(choices) // 機器人(電腦)的選擇
print("我的選擇: " + UserChoiceText(opponenetChoice))
// Judgment of victory and defeat
if opponenetChoice == str.upper(userChoice):
print("平局! ")
elif opponenetChoice == 'S' and userChoice.upper() == 'J':
print("石頭砸剪刀, 我贏了! ")
continue
elif opponenetChoice == 'J' and userChoice.upper() == 'B':
print("剪刀剪布, 我贏了! ")
continue
elif opponenetChoice == 'B' and userChoice.upper() == 'S':
print("布包石頭,我贏了! ")
continue
else:
print("你贏了!")
復制代碼