2015-11-25 35 views
-3
import random 

#Rock Paper Scisscors Lizard Spock Game 

#Name to number 
rock =() 
paper =() 
scisscors =() 
lizard =() 
Spock =() 

def main(): 
    print("Let's play rocker, paper, scissors, lizard, Spock") 
    computer = npc_guess() 
    player = user_guess() 
    complete(computer, player) 

def user_guess(): #Users hand in RPSLS 
    player = input("Choose 'rock', 'paper', 'scisscors', 'lizard', 'Spock'") 
    game = True 
    while game: 
     if player == 'rock' or player == 'paper' or player == 'scisscors'\ 
     or player == 'lizard' or player == 'Spock': 
      return player 
    else: 
     print("That Choice isn't valid.") 
user_guess() 

def npc_guess(): #computers hand in RPSLS 
    while True: 
     computer = random.randrange(0, 4) 
     if computer == 0: 
      print("The NPC has chosen rock") 
      computer == rock 
     elif computer == 1: 
      print("The NPC has chosen paper") 
      computer == paper 
     elif computer == 2: 
      print("The NPC has chosen scisscors") 
      computer == scisscors 
     elif computer == 3: 
      print("The NPC has chosen lizard") 
      computer == lizard 
     elif computer == 4: 
      print("The NPC has chosen Spock") 
      computer = Spock 
     return computer 
npc_guess() 


def complete(player, computer): #determines if the computer beat you, if not you win or tie. 
    if computer == ('rock' and player == 'lizard') or (computer == 'rock' and player == 'paper')\ 
     or (computer == 'paper' and player == 'scisscors') or (computer == 'paper' and player == 'Spock')\ 
     or (computer == 'scisscors' and player == 'paper') or (computer == 'paper' and player == 'lizard')\ 
     or (computer == 'lizard' and player == 'paper') or (computer == 'lizard' and player == 'Spock')\ 
     or (computer == 'Spock' and player == 'rock') or (computer == 'Spock' and player == 'scisscors'): 
      print("The Computer wins") 
    elif computer == player: 
     print("It was a tie") 
    else: 
     print("You win!") 

main() 

幾個問題,我找不出來。沒有我選擇的和NPC選擇的東西,它總是說用戶是贏家。當我運行程序時,它還要求我連續兩次選擇一個選項。創建一個python搖滾紙剪刀遊戲,有一些bug

+2

你不能只是解釋兩句傾倒你的整個代碼在這裏,並希望有人來下載,運行它,並把它擦亮你... –

+0

刪除函數調用你剛過函數定義有。 – hjpotter92

+0

創建一個帶有五個鍵(電腦手)的字典,兩個vals是失手(玩家)。它會讓你的代碼更清潔 – SirParselot

回答

-1

跑這個,它似乎工作。查看內嵌評論,瞭解以前不用的內容以及這些更改如何解決問題。

另外請注意,在這段代碼中仍然有一些東西需要研究,但我會留給你弄清楚。

import random 

#Rock Paper Scissors Lizard Spock Game 

def main(): 
    print("Let's play rocker, paper, scissors, lizard, Spock") 
    player = user_guess() # Switch order of player and computer so player cannot see computer guess before choosing 
    computer = npc_guess() 
    complete(computer, player) 

def user_guess(): #Users hand in RPSLS 
    player = input("Choose 'rock', 'paper', 'scissors', 'lizard', 'Spock': ") 
    game = True 
    while game: 
     if player == 'rock' or player == 'paper' or player == 'scissors'\ 
     or player == 'lizard' or player == 'Spock': 
      return player 
    else: 
     print("That Choice isn't valid.") 

def npc_guess(): #computers hand in RPSLS 
    while True: 
     computer = random.randrange(0, 4) 
     if computer == 0: 
      print("The NPC has chosen rock") 
      computer = 'rock'  # Need string assignment for all these. Had boolean evaluation before. Without assignment, computer was never taking on a value of 'rock', 'paper', 'scissors', 'lizard' or 'Spock', and what was returned was the random integer from above 
     elif computer == 1: 
      print("The NPC has chosen paper") 
      computer = 'paper' 
     elif computer == 2: 
      print("The NPC has chosen scissors") 
      computer = 'scissors' 
     elif computer == 3: 
      print("The NPC has chosen lizard") 
      computer = 'lizard' 
     elif computer == 4: 
      print("The NPC has chosen Spock") 
      computer = 'Spock' 
     return computer 


def complete(player, computer): #determines if the computer beat you, if not you win or tie. 
    if (computer == 'rock' and player == 'lizard') or (computer == 'rock' and player == 'paper')\ # Previously had if computer == ('rock' and player == 'lizard') or ... , which will always boil down to "if computer == True" since bool('rock') is true. Then "if computer == True" always evaluates to False since computer is never a boolean value 
     or (computer == 'paper' and player == 'scissors') or (computer == 'paper' and player == 'Spock')\ 
     or (computer == 'scissors' and player == 'paper') or (computer == 'paper' and player == 'lizard')\ 
     or (computer == 'lizard' and player == 'paper') or (computer == 'lizard' and player == 'Spock')\ 
     or (computer == 'Spock' and player == 'rock') or (computer == 'Spock' and player == 'scissors'): 
      print("The Computer wins") 
    elif computer == player: 
     print("It was a tie") 
    else: 
     print("You win!") 

main() 
+0

這是一個比StackOverflow答案更多的代碼審查。您還留下了原始代碼的幾個可疑部分。我認爲這不是一個好的答案,因爲這不是一個問題。 –

+0

儘管問題可能沒有以最佳方式進行框架化,但是OP指出了他/她正在遇到的具體問題(即用戶總是勝出)併發布了調查問題所需的代碼。我已經提出瞭解決原始問題的解決方案,並解釋了用戶每次獲勝的原因。我留下了一些可疑的代碼部分,因爲雖然它們可能編碼風格很差,但它們並不一定是不正確的,並且似乎與OP報告的原始問題沒有任何關係。 – rfj001

+0

我不認爲這有助於StackOverflow。我認爲這可能有助於這個特定的OP,在這個特定的時刻。然後再沒有人。這不是StackOverflow應該如何工作的。當我遇到這些問題時,我倒下並投票結束。當他們得到答案時,他們不太可能會離開,更可能會更像將來會發生的事情。你可能不同意。我甚至可能在少數。我支持我的選票。 –