Python变量未打印
我在编程类中有一个项目,用python创建一些东西,我决定制作一个pokedex。但不知道为什么当我给它输入1时,当它请求pokenumber时,它返回nonePython变量未打印,python,Python,我在编程类中有一个项目,用python创建一些东西,我决定制作一个pokedex。但不知道为什么当我给它输入1时,当它请求pokenumber时,它返回none 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
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)
将键入的内容解析为字符串。您需要使用将其转换为整数,或者您可以轻松地将其与字符串“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)
最后几行有多个问题:
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()
更好的办法是避免使用globals:
def print_userpoke_details(userpoke):
if int(userpoke) == 1:
print (Bulbasaur)
userpoke = raw_input("Pokémon #:")
print_userpoke_details(userpoke)
您应该有diff函数名。函数名和变量名相同,请尝试以下更改:
def _userpoke():
if userpoke == '1':
print (Bulbasaur)
_userpoke()
这可能会对您有所帮助。您有一个同名的变量和函数!!你必须有不同的函数名,最后必须调用,还需要将
string
转换为int
的raw\u input
。你在这里做错了太多事情,我想说你在这里提问之前需要复习一下你的教学材料。