Random Walker没有运行它应该python的次数

时间:2017-04-06 05:59:49

标签: python random random-walk

该代码应该是一个随机漫步者,运行多次用户输入,如果步行者设法到达游戏板的边缘,他赢了或者如果步行者回到他的起始位置,他输。现在我的代码运行但胜负并不等于用户估算的程序运行次数

    # Project 3 yay
#right now my win loss ratio is wrong///very off
from random import choice #choice makes a random selection from list
path = []
path_len_list = []
string_outcome = "ok"
def move_man():
    """Function moves the walker left, right, up, or down according to the random number selected"""
    directions = ["left", "down", "right", "up"]
    position = [3, 3]
    #print(path)
    path.clear()
    global string_outcome
    string_outcome = ""
    #Randomly generate a integer 1 - 4
    while string_outcome != "win" and string_outcome != "loss":
        x = choice(directions)
        path.append(list(position))
        if x == "down": #decrement x position
            x = position[0] - 1
            y = position[1]
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        elif x == "right": #increment y position
            x = position[0]
            y = position[1] + 1
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        elif x == "up": #increment x positon
            x = position[0] + 1
            y = position[1]
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        elif x == "down": #decrement y postion
            x = position[0]
            y = position[1] - 1
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        else:
            break
    write_to_file(path, string_outcome)
    return string_outcome
def check_position_1(position):
    """Checks whether the move results in a win, loss, or if the walker continues walking. Keeps tally of the amounts of wins and losses."""
    if (position[0] == 0 or position[0] == 6 or position[1] == 0 or position[1] == 6):
        print("Winning condition")
        string_outcome = "win"
        path_len = len(path)
        path_len_list.append(path_len)
        return string_outcome
    elif (position[0] == 3 and position[1] == 3):
        string_outcome = "loss"
        print("Losing condition")
        path_len = len(path)
        path_len_list.append(path_len)
        return string_outcome
    else:
        string_outcome = "ok"
        return string_outcome

def write_to_file(path, string_outcome): #right now its only writing the last case not all of them
    """Iterates through each path and writes the coordinates of every win and loses to a file"""
    with open("log.txt", "a") as my_file:
        print(string_outcome, file=my_file)# print text to my_file
        print(path, file=my_file)


def get_win_percentage(number_of_win, number_of_loss):
    """Totals wins and loses, divides the amount of wins by the total, and then displays the percentage of wins"""
    win_sum = number_of_win + number_of_loss
    win_percentage = number_of_win / win_sum
    win_percentage = round(win_percentage, 2)
    win_percentage = win_percentage * 100
    return win_percentage

def get_average_game_length(path_len_list, number_of_games):
    """Calculates the average amount of steps taken for all the games ran"""
    average_game_length = sum(path_len_list) / (number_of_games)
    print(path_len_list)
    average_game_length = round(average_game_length, 3)
    return average_game_length


def walking_game():
    "main function, gets inputs, calls other functions and prints out information to user"""
    global string_outcome
    number_of_win = 0
    number_of_loss = 0
    print("This program simulates a random walk on a grid.")
    number_of_games = int(input("How many games should be simulated? "))
    for i in range(number_of_games):
        move_man()
        if string_outcome == "win":
            number_of_win = number_of_win + 1
            #print(number_of_win)
        elif string_outcome == "loss":
            number_of_loss = number_of_loss + 1 
            #print(number_of_loss)
        else:
            pass
    win_percentage = get_win_percentage(number_of_win, number_of_loss)
    average_game_length = get_average_game_length(path_len_list, number_of_games)
    print("There were", number_of_win, "number of wins and", number_of_loss, "number of losses, resulting in a win percentage of", win_percentage, "%")
    print("The average game length was", average_game_length)
    print("Log of games written to log.txt")


walking_game()

1 个答案:

答案 0 :(得分:1)

您需要检查if x == "down":两次。将第一个更改为if x == "left":

调试未来的提示: 将else: breakelse: pass替换为else: assert False以注意此类错误。