2017-09-06 104 views
-4

我想用一個代碼隨機模塊我目前正在設法解決:Python的隨機模塊功能

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

+0

什麼'random_computer_choice'應該做什麼?它預計不會有任何爭論。 – Grigoriy

+1

對「隨機」模塊有什麼不瞭解?一個簡單的'random.randint(0,2)'會得到你想要的結果。 – abccd

回答

4

OP有比較奇怪的任務。但我認爲OP的意思是這樣的:

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