2017-03-31 12 views
0

を返すプログラムは、次のコードを実行しようとすると、ユーザがジェンダーを割り当てようとして整数を入力したときに変数が未定義であることを返します。どうやってこれを修正しますか?未定義変数(Python)

#define the function for the physical characteristics question 
def physical_characteristics_question (question, answer1, answer2, answer3) : 
    print (question) 
    print ('1. ' + answer1) 
    print ('2. ' + answer2) 
    print ('3. ' + answer3) 

    while True: 
     variable = (input('Please enter a number: ')) 
     while True: 
     #repeats question if a type other than integer is entered 
      try: 
       int(variable) 
       break 
      except ValueError: 
       variable = 0 
       continue 

     #repeats question if a number out of range is entered 
     if int(variable) >= 1 and int(variable) <= 3: 
      break 

    return int(variable) 


physical_characteristics_question ("What is your gender?", 'male', 'female', 'other') 

if (variable) == 1: 
    gender = 'male' 
elif (variable) == 2: 
    gender = 'female' 
else: 
    gender = 'other' 

ありがとうございました!

+0

戻り値は何にも割り当てられませんでした。 'variable = physical_characteristics_question("あなたの性別はなんですか? "、...)'を実行します。関数内で代入する変数は、その関数内でのみ使用できます。 – Ryan

+0

固定、ありがとう! –

答えて

0

あなたの関数physical_characteriscs_questionは、結果を変数に戻していません。これを試してください:

variable = physical_characteristics_question ("What is your gender?", 'male', 'female', 'other')