2017-01-04 13 views
-1

私はPythonで何かを作成するプログラミングクラスのプロジェクトを持っており、私はpokedexを作ることにしました。私は1の入力を与えるときにそれが何も返さないポケベルを求めるとき、なぜわからないのですか?Python変数が印刷されない

import random 
import time 

print "Hello new Trainer!" 
time.sleep(1.6) 
print "I am your Kanto region Pokédex" 
time.sleep(2.3) 
print "Please enter your name below so I may know what to call you." 
time.sleep(2) 
name = raw_input("Name:") 
time.sleep(1) 
print "Hello %s, it is nice to meet you" % (name) 
time.sleep(2) 
print "I am a Pokédex, a Pokédex is a database of Pokémon." 
time.sleep(3) 
print "This Pokédex is specific for Pokémon in the Kanto region." 
time.sleep(3.5) 
print "All Pokémon have an assigned number that corresponds to that   certain Pokémon species" 
time.sleep(4) 
print "For example, Pikachu is the 25th entry in the Pokédex!" 
time.sleep(3) 
print "When you enter a Pokémon's # it will bring up all available information on that Pokémon" 
time.sleep(5) 
print "Please enter a number between 1 and 151 to learn about the Pokémon associated to that number." 
Bulbasaur = "Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger." 

userpoke = raw_input("Pokémon #:") 
def userpoke(): 
    if userpoke == 1: 
    print (Bulbasaur) 
+2

同じ名前の変数と関数があります。 –

+0

diff関数名を持っていなければなりません。最後にコールする必要があります。また、 'string'を' raw_input'の 'int'にキャストする必要があります。 – Devansh

+0

あなたは今ここで質問をする前に、教材を見直す必要があると言っています。 – TigerhawkT3

答えて

0

最後の数行で複数の問題があります:

​​3210

これは、ユーザー入力から文字列を読み込み、変数userpokeに保存し、私はあなたがこのような何かをお勧めします。

def userpoke(): 
    if userpoke == 1: 
    print (Bulbasaur) 

これは、以前に作成された変数userpokeを上書きし、自身の機能オブジェクトは、この関数は、とも呼ばれることはない整数で1に等しいかどうかをチェックする機能に置き換え。

代わりに以下を試してください。これは関数に別の名前を使用するため、以前に作成された変数を上書きしないように、userpokeを整数と比較する前に整数に変換してから実際に関数を呼び出します。

userpoke = raw_input("Pokémon #:") 

def print_userpoke_details(): 
    if int(userpoke) == 1: 
    print (Bulbasaur) 

print_userpoke_details() 

さらに良いことには、グローバルの使用を避けるために、次のようになります。

def print_userpoke_details(userpoke): 
    if int(userpoke) == 1: 
    print (Bulbasaur) 

userpoke = raw_input("Pokémon #:") 
print_userpoke_details(userpoke) 
+0

omgありがとうございます<3 –

1

raw_input()文字列として入力する内容を解析します。 int()を使用して整数にキャストする必要があります。または、簡単な方法で整数1の代わりに"1"の文字列と比較することもできます。

編集:コメンターはちょうど指摘したように、あなたも同じ名前の変数と関数があります。この場合

userpoke = raw_input("Pokémon #:") 
def userpoke(): 
    if userpoke == 1: 
    print (Bulbasaur) 

を、あなたのif文でuserpokeが実際に機能を指し、ありません変数。

def userpoke(): 
    pkmn_num = raw_input("Pokémon #:") 
    if pkmn_num == "1": 
    print (Bulbasaur) 
0

あなたは差分関数名を持つ必要があります。関数名と変数名が同じであれば、次のように変更してみてください。

def _userpoke(): 
    if userpoke == '1': 
    print (Bulbasaur) 

_userpoke() 

これは役立つかもしれません。

関連する問題