2016-06-12 9 views
1

既存の質問をよく見て、まったく同じシナリオを見ていない。私はPythonの基礎を学び、ユビキタスなRock、Paper、Scissorsゲームを作成しようとしています。しかし、私はコンピュータの推測を表すランダムに選択されたアイテムとの比較に成功できないユーザーの推測に等しい変数を持っています。 2つの変数が決して等しくないことを示唆してはいけないときでも、すべてが「あなたが勝ちます!:)」と評価されます。私は運がないstr(computer_choice)を使ってみました。Python - 文字列とリストのランダムな項目を比較する

は、ここでは、コードの全体です:

import time 
import random 

rps = ['Rock', 'Paper', 'Scissors'] 

computer_choice = random.sample(rps, 1) 

print("Let's play some Rock, Paper, Scissors") 
print("Hit enter when ready:") 
input("") 

print("Rock...") 
time.sleep(1) 
print("Paper...") 
time.sleep(1) 
print("Scissors...") 
time.sleep(.75) 
print("SHOOT!") 

print("Which is it?") 
user_choice = input(">>>") 



while True: 
    valid_check = user_choice in rps 
    if valid_check == True: 
     break 
    else: 
     print('Bad input, try again:') 
     user_choice = input(">>>") 

print("The computer picked: %s" % computer_choice) 

if user_choice == computer_choice: 
    print("It's a draw!") 

elif user_choice == 'Rock': 
    if computer_choice == 'Paper': 
     print("You lose :(") 

    else: 
     print("You win! :)") 

elif user_choice == 'Paper': 
    if computer_choice == 'Scissors': 
     print("You lose :(") 

    else: 
     print("You win! :)") 

elif user_choice == 'Scissors': 
    if computer_choice == 'Rock': 
     print("You lose :(") 

    else: 
     print("You win! :)") 

答えて

3

はあなたではなく、元のリストからサイズ1のリストを返しますrandom.sample(rps,1)よりも、元のリストの要素を返しrandom.choice(rps)を使用する必要があります。

+0

完璧に働いて説明が合理的です。私がリストを常に偽となる文字列と比較していたと思います。ありがとう! – mannayz

関連する問題