पायथन का उपयोग करके हम बहुत दिलचस्प गेम भी विकसित कर सकते हैं। रॉक पेपर कैंची गेम उनमें से एक है। यहाँ हम रैंडम नंबर उत्पन्न करने के लिए randint() फ़ंक्शन का उपयोग करते हैं।
इस खेल में खिलाड़ी आमतौर पर तीन तक गिनने की अनुमति देते हैं, या खेल का नाम बोलते हैं, प्रत्येक बार या तो एक हाथ को मुट्ठी में उठाकर गिनती पर नीचे झूलाते हैं या इसे पीछे पकड़े रखते हैं।
उदाहरण कोड
# आवश्यक रैंडम मॉड्यूल इम्पोर्ट करना
import random
print("The Rules of Rock paper scissor game will be follows: \n" +
"Rock vs paper --> paper wins \n" +
"Rock vs scissor --> Rock wins \n" +
"paper vs scissor --> scissor wins \n")
while True:
print("Now please enter your choice no. \n 1. Rock \n 2. paper \n 3. scissor \n")
# उपयोगकर्ता से इनपुट लें
ch = int(input("Now Your turn: "))
while ch > 3 or ch < 1:
ch = int(input("Enter your valid input here: "))
if ch == 1:
choice_name = 'Rock'
elif ch == 2:
choice_name = 'paper'
else:
choice_name = 'scissor'
# उपयोगकर्ता द्वारा दी गई पसंद प्रिंट करें
print("Your choice is: " + choice_name)
print("\nNow its computer turn to initiate.......")
# कंप्यूटर रैंडम मॉड्यूल के randint मेथड का उपयोग करके
# मान 1, 2 और 3 में से बेतरतीब ढंग से कोई भी संख्या चुनेगा
comp_choice = random.randint(1, 3)
# लूप तब तक जारी रहेगा जब तक comp_choice मान
# चॉइस मान के बराबर है
while comp_choice == ch:
comp_choice = random.randint(1, 3)
# चॉइस मान के अनुरूप comp_choice_name वेरिएबल का मान आरंभ करें
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'paper'
else:
comp_choice_name = 'scissor'
print("So computer choice is: " + comp_choice_name)
print(choice_name + " V/s " + comp_choice_name)
# गेम जीतने की शर्त
if ((ch == 1 and comp_choice == 2) or
(ch == 2 and comp_choice == 1)):
print("paper wins => ", end="")
final_result = "paper"
elif ((ch == 1 and comp_choice == 3) or
(ch == 3 and comp_choice == 1)):
print("Rock wins =>", end="")
final_result = "Rock"
else:
print("scissor wins =>", end="")
final_result = "scissor"
# या तो उपयोगकर्ता या कंप्यूटर जीतता है प्रिंट करना
if final_result == choice_name:
print("<== You are the winner ==>")
else:
print("<== Computer wins ==>")
print("Do you want to play again? (Y/N)")
ans = input()
# यदि उपयोगकर्ता इनपुट n या N है तो शर्त सत्य है
if ans == 'n' or ans == 'N':
break
# व्हाइल लूप से बाहर निकलने के बाद
print("\nThanks for sharing time with us...")
आउटपुट
The Rules of Rock paper scissor game will be follows: Rock vs paper --> paper wins Rock vs scissor --> Rock wins paper vs scissor --> scissor wins Now please enter your choice no. 1. Rock 2. paper 3. scissor Now Your turn: 1 Your choice is: Rock Now its computer turn to initiate....... So computer choice is: paper Rock V/s paper paper wins => <== Computer wins ==> Do you want to play again? (Y/N) y Now please enter your choice no. 1. Rock 2. paper 3. scissor Now Your turn: 2 Your choice is: paper Now its computer turn to initiate....... So computer choice is: Rock paper V/s Rock paper wins => <== You are the winner ==> Do you want to play again? (Y/N) n Thanks for sharing time with us...