Python随机模块函数

时间:2017-09-06 05:18:58

标签: python python-3.x random

我想将随机模块用于我目前正在尝试解决的代码:

def choice_to_number(choice):
    """Convert choice to number."""
    # If choice is 'rock', give me 0
    # If choice is 'paper', give me 1
    # If choice is 'scissors', give me 2
    if choice == 'rock':
        return 0
    elif choice == 'paper':
        return 1
    else:
        return 2

def number_to_choice(number):
    """Convert number to choice."""
    # If number is 0, give me 'rock'
    # If number is 1, give me 'paper'
    # If number is 2, give me 'scissors'
    if number == 0:
        return 'rock'
    elif number == 1:
        return 'paper'
    else:
        return 'scissors'

def random_computer_choice():
    """Choose randomly for computer."""

需要找到随机部分的代码才能解决它

完整代码:http://www.codeskulptor.org/#user43_rc6Pm3n7uj_0.py

1 个答案:

答案 0 :(得分:4)

OP有相当奇怪的任务。但我认为OP意味着这样的事情:

def random_computer_choice():
    """Choose randomly for computer."""
    return random.choice(['rock','paper','scissors'])