2015-08-17 70 views
-5

此代碼通過正常運行一次,然後帶來的誤差斷碼,需要幫助固定

Traceback (most recent call last): 
    File "<pyshell#2>", line 1, in <module> 
    paper 
NameError: name 'paper' is not defined 

我需要的代碼能夠運行,石頭,蜥蜴,斯波克運行10次的遊戲顯示10分(或選定的數量)比賽結束時的得分(因爲它是和)。 下面是代碼:

import random 

numberofgames = raw_input("How many games do you want to play? ") 

print "Please choose rock , paper , scissors , lizard, or spock (in lower case please)" 
choice = raw_input("What do you choose? ") 
player_choice = str(choice) 
def name_to_number(name): 
    if name == "rock": 
     name = 0 
     return name 
    elif name == "spock": 
     name = 1 
     return name 
    elif name == "paper": 
     name = 2 
     return name 
    elif name == "lizard": 
     name = 3 
     return name 
    elif name == "scissors": 
     name = 4 
     return name 

def number_to_name(number): 
    if number == 0: 
     number = "rock" 
     return number 
    elif number == 1: 
     number = "spock" 
     return number 
    elif number == 2: 
     number = "paper" 
     return number 
    elif number == 3: 
     number = "lizard" 
     return number 
    elif number == 4: 
     number = "scissors" 
     return number 

try: 
    computer_choice = random.randrange(5) 
    player_number = name_to_number(player_choice) 
    print "Player choice is: " + player_choice 
    print "Computer choice is: " + number_to_name(computer_choice) 
    difference = (int(player_number) - computer_choice) % 5 
    draws = 0 
    playerwins = 0 
    computerwins = 0 
    if difference in [1, 2]: 
     print "Player wins!" 
     playerwins = playerwins + 1 
    elif difference == 0: 
     print "Player and computer tie!" 
     draws = draws + 1 
    else: 
     print "Computer wins!" 
     computerwins = computerwins + 1 

    print "Wins: " + str(playerwins) + "\n" + "Draws: " + str(draws) + "\n" + "Losses " + str(computerwins) 

    while playerwins + draws + computerwins <= numberofgames: 
     name_to_number() 

except TypeError: 
    print "Sorry, please read the directions and type rock, paper, scissors, spock, or lizard in lowercase." 
+0

這是你的整個代碼?這個錯誤似乎不是來自這段代碼。 – DeepSpace

+0

您昨天問過這個,並將其刪除了 – muddyfish

+0

歡迎來到Stack Overflow!這個錯誤意味着你在使用'paper'作爲變量之前告訴Python它應該保持什麼價值,但我沒有看到你在這個腳本中這樣做。你在這個腳本的同一目錄下是否有一個名爲'random.py'的文件? – SuperBiasedMan

回答

0

您發佈的代碼引起TypeError: name_to_number() takes exactly 1 argument (0 given)因爲你打電話給你name_to_number功能,不帶參數。

順便說一句,這裏是該函數的簡化版本:

name_to_number_dict = dict(rock=0, spock=1, paper=2,lizard=3,scissors=4) 

def name_to_number(name): 
    if name not in name_to_number_dict: 
    raise ValueError("illegal name") 
    return name_to_number_dict[name] 

編輯:

其實你並不需要這樣的功能都沒有。一個更簡單的方法可能如下:

import random 

data = "rock", "spock", "paper", "lizard", "scissors" 

def playgames(): 
    tally = dict(win=0, draw=0, loss=0) 
    numberofgames = raw_input("How many games do you want to play? ") 
    numberofgames = int(numberofgames) 
    for _ in range(numberofgames): 
    outcome = playgame() 
    tally[outcome] += 1 
    print """ 
    Wins: {win} 
    Draws: {draw} 
    Losses: {loss} 
    """.format(**tally) 

def playgame(): 
    choice = "" 
    while (choice not in data): 
    choice = raw_input("Enter choice (choose rock , paper , scissors , lizard, or spock):") 
    choice = choice.lower() 
    print "Player choice is:{}".format(choice) 
    player_number = data.index(choice) 
    computer_number = random.randrange(5) 
    print "Computer choice is: {}".format(data[computer_number]) 
    difference = (player_number - computer_number) % 5 
    if difference in [1, 2]: 
    print "Player wins!" 
    outcome = "win" 
    elif difference == 0: 
    print "Player and computer tie!" 
    outcome = "draw" 
    else: 
    print "Computer wins!" 
    outcome = "win" 
    return outcome 

playgames() 
+0

我是新來的編碼所以我會怎麼樣把這個寫入代碼中? – Bruh

+0

哦等待dw,我想通了,但問題仍然存在:( – Bruh

+0

你是否修復該函數調用? – Alan