मैं अजगर सीख रहा हूं और मैं 2 खिलाड़ी पासा-रोलिंग गेम बना रहा हूं लेकिन मैं खिलाड़ियों को रोल करने या दूसरे दौर में खेलने की अनुमति नहीं दे सकता। यह वह कोड है जिसे मैंने लिखा है। अगर कोई मुझे कोड को सही करने में मदद कर सकता है तो मैं इसकी सराहना करता हूं। धन्यवाद!
import random
import time
player_a = input("Player1, please type your name:")
time.sleep(1)
player_b = input("Player2, please type your name:")
time.sleep(1)
print("Welcome to Dice Roller", player_a, "and", player_b)
score_a = 0
score_b = 0
roll_a = False
roll_b = False
while not (roll_a and roll_b):
ans_a = input("Type roll to roll the dice:")
if ans_a == "roll":
roll_a = (random.randint(1, 6))
time.sleep(1)
print(player_a, "has rolled", roll_a)
else:
print("Please check you spelling")
time.sleep(2)
ans_b = input("Your turn to roll, type roll to roll the dice:")
if ans_b == "roll":
roll_b = (random.randint(1, 6))
time.sleep(1)
print(player_b, "has rolled", roll_b)
if roll_a == roll_b:
time.sleep(2)
print("Tie")
break
roll_a = False
roll_b = False
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
break
roll_a = False
roll_b = False
if roll_b > roll_a:
score_b = roll_b - roll_a
print(player_b, "has scored", score_b, "points")
break
roll_a = False
roll_b = False
2 जवाब
हो सकता है कि आप अपने गेम को ऐसे फ़ंक्शन में दोबारा करना चाहें जो उपयोगकर्ता से "रोल" टाइप करने का अनुरोध करता है और रोल परिणाम देता है, जैसा कि नीचे दिया गया है।
मुख्य while True
लूप कभी खत्म नहीं होगा (हालाँकि आप इसे ५ राउंड खेलने के लिए for round in range(5):
जैसा बना सकते हैं), और request_roll()
लूप को return
से तोड़ दिया जाएगा। .
import random
import time
def request_roll(name):
while True:
ans = input(f"{name}, type roll to roll the dice:").lower()
if ans == "roll":
roll = random.randint(1, 6)
time.sleep(1)
print(name, "has rolled", roll)
return roll
print("Please check your spelling")
time.sleep(2)
player_a = input("Player1, please type your name:")
player_b = input("Player2, please type your name:")
print("Welcome to Dice Roller", player_a, "and", player_b)
while True:
roll_a = request_roll(player_a)
roll_b = request_roll(player_b)
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
elif roll_b > roll_a:
score_b = roll_b - roll_a
print(player_b, "has scored", score_b, "points")
else:
print("It's a draw")
break
स्टेटमेंट तुरंत लूप को छोड़ देता है इसलिए पहले break
के बाद का कोड कभी नहीं चलता। अधिक जानकारी के लिए यहां दस्तावेज़ देखें: https://www.tutorialspoint.com/python/python_break_statement। एचटीएम
आपके कोड के साथ कई अन्य मुद्दे भी हैं जो अभी तक दिखाई नहीं दे रहे हैं (क्योंकि कोड का वह भाग कभी नहीं चलता है):
निम्नलिखित अनुभाग में roll_a
और roll_b
को False
से अधिलेखित कर दिया जाता है। इसका मतलब है कि अगर-स्टेटमेंट में तुलना काम नहीं करेगी। आप score_a
को भी ओवरराइट कर रहे हैं, जहां मुझे लगता है कि आप इसे जोड़ना चाहेंगे (=
के बजाय +=
ऑपरेटर का उपयोग करें)।
roll_a = False
roll_b = False
if roll_a > roll_b:
score_a = roll_a - roll_b
print(player_a, "has scored", score_a, "points")
मैं निम्नलिखित की तरह कुछ सुझाव दूंगा (ध्यान दें कि मैंने इसका परीक्षण नहीं किया है!):
play_another_round = True
while play_another_round:
ans_a = input("Type roll to roll the dice:")
if ans_a == "roll":
roll_a = (random.randint(1, 6))
time.sleep(1)
print(player_a, "has rolled", roll_a)
else:
print("Please check you spelling")
time.sleep(2)
ans_b = input("Your turn to roll, type roll to roll the dice:")
if ans_b == "roll":
roll_b = (random.randint(1, 6))
time.sleep(1)
print(player_b, "has rolled", roll_b)
if roll_a == roll_b:
print("Tie")
elif roll_a > roll_b:
score_a += roll_a - roll_b
print(player_a, "has scored", score_a, "points")
elif roll_b > roll_a:
score_b += roll_b - roll_a
print(player_b, "has scored", score_b, "points")
continue_game_check = input("Do you want to play another round (Yes/No)?")
play_another_round = continue_game_check == "Yes"
print("The game has ended")
print(player_a, "has scored", score_a, "points")
print(player_b, "has scored", score_b, "points")
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।