मुझे यहाँ समस्या है। मैं पायथन में नया हूँ। मैं एक मिनी गेम नाम डाइस रोल बनाना चाहता हूं। नियम इस प्रकार है:
रोल की संख्या असीमित है
खिलाड़ियों की संख्या असीमित है (असीमित का मतलब है कि उपयोगकर्ता जितने चाहें उतने खिलाड़ी और रोल जोड़ सकता है)
फिर प्रत्येक खिलाड़ी के लिए कुछ प्रयासों के बाद, यह कुल पासे की गणना करेगा कि वे कितनी बार एक रोल बनाते हैं
फिर प्रोग्राम गणना करेगा कि किस खिलाड़ी का कुल योग सबसे अधिक है और विजेता बन जाता है।
यह मेरा कोड है और मैं वर्तमान में बिंदु 4 पर अटका हुआ हूं।
import random
numPlayer = int(input("Enter number of player:"))
numTest = int(input("Enter the number of test:"))
def dice_roll():
total = 0
for i in range(numTest):
nana = random.randint(1 , 6)
total = total + nana
#print("TOTAL: " + str(total))
return total
player = 0
for j in range(numPlayer): # number of player
print("\n")
print("Player " + str(j + 1))
print("-------")
print(dice_roll())
# create a variable to store total for each player
1 उत्तर
पायथन में हमारे पास एक डेटा प्रकार होता है जिसे एक शब्दकोश कहा जाता है जो कुंजी/मूल्य जोड़े जैसे नाम/स्कोर संग्रहीत करता है जो आपके प्रोग्राम के लिए बिल्कुल सही है। मैंने आपके कोड में हाईस्कोर नामक एक शब्दकोश जोड़ा है और यह अब बहुत अच्छा काम करता है और (स्कोर, प्लेयर) जोड़ी को प्रिंट करता है ताकि आप जान सकें कि कौन जीता और उनका स्कोर क्या था। यह अभी भी अन्य खिलाड़ियों को भी प्रिंट करता है ताकि आप इसे देख सकें:
import random
def dice_roll(rolls):
total = 0
for i in range(rolls):
nana = random.randint(1, 6)
total = total + nana
print("TOTAL :" + str(total))
return total
# gather user parameters
numPlayer = int(input("Enter number of player:"))
numTest = int(input("Enter the number of test:"))
# initialize a dictionary to store the scores by player name
highScore = {}
player = 0
for j in range(numPlayer):
print("\n")
name = ("Player "+str(j+1)) # assign the name to a string variable
print(name)
print("-------")
score = dice_roll(numTest) # store the value in an int variable
print(str(score))
highScore[name] = score # store the name and score in a dictionary
# prints the high score and the player (score, player)
print(max(zip(highScore.values(), highScore.keys())))
मैं इस उम्मीद में हूँ की इससे मदद मिलेगी। अधिकतम ज़िप मूल्य के आधार पर क्रमबद्ध होगा और मूल्य और अधिकतम मूल्य की कुंजी दोनों को प्रिंट करेगा।
संबंधित सवाल
नए सवाल
python-3.x
पायथन प्रोग्रामिंग के बारे में प्रश्नों के लिए जो भाषा के संस्करण 3+ के लिए विशिष्ट हैं। सभी पायथन सवालों पर अधिक जेनेरिक [अजगर] टैग का उपयोग करें, और केवल यह जोड़ें यदि आपका प्रश्न संस्करण-विशिष्ट है। पायथन 2 प्रश्नों के लिए [अजगर -2] टैग का उपयोग करें।