# Version 2 To Distribute in Class: # # Change this version to have a variable size back yard. # # A Mess in the Backyard # This Python code uses the Battleship code to simulate # location of dog poo in the backyard. # May 1, 20014 # Import the random number generator. from random import randint # Set up the yard. This yard will have n rows and n columns. # Ask the player how big the backyard is. yard_size = int(raw_input("How big is the backyard?")) board = [] for x in range(yard_size): board.append(["O"] * yard_size) # This function prints the board with spaces between each value in the row. def print_board(board): for row in board: print " ".join(row) # Greet the user and print out the board. print "Did you clean the yard?" #print_board(board) def random_row(board): return randint(0, len(board) - 1) def random_col(board): return randint(0, len(board[0]) - 1) # Call the random number generator function to locate the mess. ship_row = random_row(board) ship_col = random_col(board) # ship_row and ship_col have the location of the ship. # Delete (or comment out) this print statement before playing the game. print "eeee-ew, watch out for the mess: Row ", ship_row, \ " Column ", ship_col # print ship_row # print ship_col # Ask the player how many times he will walk in the yard today. # Each trip will consist of 200 steps. Check each step to see if # it matches location of dog mess. # # The number of trips will be the for-loop n_trips = int(raw_input("How many trips into the backyard?")) # Be sure to indent four spaces! for trip in range(n_trips): print " " print " Trip", trip + 1 if trip == n_trips-1: print " This is your last trip." for i in range(200): step_row = random_row(board) step_col = random_col(board) # Check if player's step matches location of mess. if step_row == ship_row and step_col == ship_col: print "Gross! You need to clean your shoes!" print " Trip , ", trip+1, " Step ", i+1 break else: if(board[step_row][step_col] == "X"): print "You stepped there already." else: print "You missed the mess!" board[step_row][step_col] = "X" # print_board(board) print "Game over."
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question