2017-01-04 1 views
-1

저는 프로그래밍 수업에서 파이썬으로 무언가를 만드는 프로젝트를 가지고 있고 pokedex를 만들기로했습니다. 하지만 그것을 왜 pokenumber 그것을 묻는 때 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

: 나는 당신이 이런 식으로 뭔가 할 것을 권 해드립니다이 사용자 입력에서 문자열을 읽어

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

를, 그리고 변수 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

감사합니다. <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

당신은 DIFF 함수 이름이 있어야합니다. 함수 이름과 변수 이름이 같으므로 다음 변경을 시도하십시오.

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

_userpoke() 

이 경우 도움이 될 수 있습니다.

관련 문제