मैं वर्तमान में इस छोटे कोड को काम करने की कोशिश कर रहा हूं। केवल एक चीज जो तब होती है जब मैं इसे निष्पादित करता हूं, वह है cmd प्रॉम्प्ट कहता है कि बंद करने के लिए एंटर दबाएं। मैं बाकी स्क्रिप्ट को चलाने और दिखाने के लिए कैसे प्राप्त कर सकता हूं? मैं स्कूल के लिए एक शुरुआती कक्षा में हूँ। इसे सुपर ओवर सरलीकृत करने की आवश्यकता नहीं है। अधिकांश भाग के लिए, यह सिर्फ ३ कमरे हैं और मुझे ३ कमरों के माध्यम से आगे और पीछे जाने में सक्षम होने की आवश्यकता है और बस इतना ही।
# A dictionary for the simplified dragon text game
# The dictionary links a room to other rooms.
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def movebetweenrooms(player, direction):
"""
Returns the new player state after moving into another room
"""
current_room = player
# check if there is a room in the specified direction
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
# error handling
else:
print("There is nothing in that direction!")
# return the player state
return current_room
def displayinstructions():
"""
display the starting instructions
"""
print("Simplified Dragon Text Game\n\n"
"Collect 6 items to win the game, or be killed by the Dragon.\n"
"Move commands: go South, go North, go East, go West\n")
def main():
displayinstructions()
player = ("Great Hall", [])
while True:
current_room = player
# Player info
# -----------
print(f"\nYou are in the {current_room}")
# Game ending
# -----------
if player[0] == "Cellar":
if len(player[1]) == 0:
print(f"Congratulations! You have defeated the Dragon!")
else:
print("NOM NOM...GAME OVER!")
# greeting and exiting
print("Thanks for playing the game. Hope you enjoyed it.")
break
# input validation and parsing
# ----------------------------
# get the player's move
print("-" * 35)
move = input("Enter your move:\n")
# invalid move syntax
if " " not in move:
print("Invalid input!")
continue
# split the move into an action and an argument
action, arg = move.split(" ", 1)
# move into another room
if action == "go":
movebetweenrooms(player, arg)
# invalid action
else:
print("Invalid input!")
input('Press ENTER to exit')
1 उत्तर
पाइथन में main
फ़ंक्शन स्वचालित रूप से निष्पादित नहीं होता है।
यदि आप इसे चलाना चाहते हैं तो इसे अपनी स्क्रिप्ट के अंत में जोड़ें:
if __name__ == '__main__':
main()
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।