程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python starts with rock paper scissors

編輯:Python

我正在參加「創意開發 投稿大賽」詳情請看:掘金創意開發大賽來了!

1. 概述

昨天寫了生命游戲,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~~

總而言之,其規則如下:

  • Rock restrains scissors
  • Scissors restrain cloth
  • Booker Stone

In order to complete the game flow smoothly,我們需要做到以下幾點:

  • You can enter your own choices through human-computer interaction
  • Set up the computer robot to make its own choices
  • Play the competition,Get the outcome of the game

2. 代碼

總體代碼如下所示:

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("你贏了!")
復制代碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved