2011-07-01 9 views
-2

メインプログラムNameError -

# import statements 
import random 
import winning 

# Set constants 
win = 0 
lose = 0 
tie = 0 

ROCK = 1 
PAPER = 2 
SCISSOR = 3 

# Main Program for the Rock Paper Scissor game. 
def main(): 
    # set variable for loop control 
    again = 'y' 

    while again == 'y': 
     # Display menu 
     display_menu() 
     # prompt for user input 
     userSelection = input('Which would you like to play with (1, 2, 3)?: ') 
     computerSelection = random.randint(1, 3) 

     # Call winner module to decide the winner! 
     print(winning.winner(userSelection, computerSelection)) 

     # Ask to play again and make selection 
     again = input('Would you like to play again (y/n)?') 


def display_menu(): 
    print('Please make a selection: ') 
    print(' 1) Play Rock') 
    print(' 2) Play Paper') 
    print(' 3) Play Scissor') 



# Call main 
main() 

セカンドファイルdefiniedない:winning.py:私は引用符とすべてを試してみました

Traceback (most recent call last): 
    File "C:/Python32/RPS_Project/Main.py", line 14, in <module> 
    ROCK = r 
NameError: name 'r' is not defined 

# This module will decide on who won based on input from Main 

def winner(userInput, computerInput): 
    if userInput == ROCK and computerInput == SCISSOR: 
     print('You win! Rock crushes Scissor!') 
     win += 1 
    elif userInput == SCISSOR and computerInput == PAPER: 
     print('You win! Scissor cuts Paper!') 
     win += 1 
    elif userInput == PAPER and computerInput == ROCK: 
     print('You win! Paper covers Rock!') 
     win += 1 
    elif userInput == computerInput: 
     print('You tied with the computer! Please try again!') 
     tie += 1 
    else: 
     print('You lost! Please try again!') 
     lose += 1 

エラーを、とは理解できませんこれ!これに関する助け? していただきありがとうございます!

+0

宿題をもう一度... –

+0

一貫した情報を提供してください。トレースバックにはコードと共通点がありません。もう一度:私たちはあなたの宿題をしていません。 –

+4

@Alli Err ... "ROCK = r"は投稿したコードにありません。実際に壊れたコードを投稿してください。投稿されると、ROCKを1に設定するので、うまく動くはずです。 –

答えて

2

不適切な言葉を間違って入力しないでください。宿題を宿題としてマークし、投稿したコードによって生成された実際のエラーを貼り付けるようにしてください。このエラーは投稿したコードと一致しません。

また少し落ち着い-見せかけの方法であなたの質問をするかもしれません:)

問題は非常に簡単です。

if userInput == ROCK and computerInput == SCISSOR: 
    print('You win! Rock crushes Scissor!') 
    win += 1 

ような行がROCKSCISSORwinが定義されていないので、NameErrorsを引き起こすしようとしているので、あなたは、メインプログラムではなく、winning.pyであなたのグローバルを定義します。すべてのモジュールで、で、使用するすべての名前を定義またはインポートする必要があります。モジュール間で名前が自動的に共有されることはありません。

returnの値がwinning.winnerである必要があります。そうしないと、期待した結果が得られません。

+0

ありがとうございます!私はすべての助けが今からタイトルの宿題を持っていることを確認します。 –

+0

ありがとうSenderle ...あなたはとても役に立ちました。 –