我正在为学校做一个迷你项目。我试着调用我已定义的函数。它工作但事情是我在第40行调用函数,函数定义在第56行。为什么它工作?是因为整个编译器和解释器都不对吗?
我在脚本模式下使用PyCharm。
def game_intro():
print "------------------ First Hero ------------------"
global n1
n1 = raw_input("Please Type a Name for Your Hero: ")
while not valid_name(n1):
print "------------------ First Hero ------------------"
n1 = raw_input("Please Type a Name for Your Hero: ")
print "------------------ Second Hero ------------------"
global n2
n2 = raw_input("Please Type a Name for Your Hero: ")
while not valid_name(n2):
print "------------------ Second Hero ------------------"
n2 = raw_input("Please Type a Name for Your Hero: ")
while not no_repeat(n2):
n2 = raw_input("Please Type a Name for Your Hero: ")
def valid_name(n):
if n == " ": # TO AVOID THE PLAYER GIVING A NAME AS SPACE
print "Your name can't be empty"
return False
elif n == "":
return False
return True
def no_repeat(m): # TO MAKE SURE THE PLAYERS WON'T TAKE THE SAME NAME
if m == n1:
print m + " Already Assigned, Please Type a Different Name."
return False
return True
答案 0 :(得分:0)
您似乎忘记了在函数中缩进代码。
您还应添加if __name__ == '__main__'
行,以便您可以像脚本一样运行代码:
python test.py
请尝试以下代码:
test.py
def game_intro():
print "------------------ First Hero ------------------"
global n1
n1 = raw_input("Please Type a Name for Your Hero: ")
while not valid_name(n1):
print "------------------ First Hero ------------------"
n1 = raw_input("Please Type a Name for Your Hero: ")
print "------------------ Second Hero ------------------"
global n2
n2 = raw_input("Please Type a Name for Your Hero: ")
while not valid_name(n2):
print "------------------ Second Hero ------------------"
n2 = raw_input("Please Type a Name for Your Hero: ")
while not no_repeat(n2):
n2 = raw_input("Please Type a Name for Your Hero: ")
def valid_name(n):
if n == " ": # TO AVOID THE PLAYER GIVING A NAME AS SPACE
print "Your name can't be empty"
return False
elif n == "":
return False
return True
def no_repeat(m): # TO MAKE SURE THE PLAYERS WON'T TAKE THE SAME NAME
if m == n1:
print m + " Already Assigned, Please Type a Different Name."
return False
return True
if __name__ == '__main__':
game_intro()
答案 1 :(得分:0)
关于如何调用函数而不用担心顺序的简单答案是使用if __name__ ==“ __main__”,例如
def fun2():
pass
def fun1():
pass
if __name__ == "__main__"
func1()
func2()
如前所述。但是,如果我是您,我将使用类进行完全控制。我会做类似的事情:
import random
class Hero:
def __init__(self):
# Game Intro
print('-'*18,'First Hero','-'*18)
# First Hero
while True:
n1 = input('Type Your Hero\'s name: ')
# For beauty and rejecting Jim, jim
self.n1 = n1.capitalize()
if self.n1.split():
break
print('Invalid Hero\'s name\n')
print('-'*18,'Second Hero','-'*18)
# Second Hero
while True:
n2 = input('Type Your Second Hero\'s name: ')
self.n2 = n2.capitalize()
if self.n2.split():
if self.n1 == self.n2:
print('\nOh! We have a hero by name',self.n2)
self.n2 = print('Change Second Hero\'s name: ')
else:
break
print('Invalid Hero\'s name\n')
# just a random silly game
def chance_game(self):
win = ['Awesome','Bravo','You Rock']
los = ['Next time', 'Almost won','Never quit']
p1 = random.randint(1,10)
p2 = random.randint(1,10)
if p1 > p2:
print(random.choice(win),self.n1)
print(random.choice(los),self.n2)
else:
print(random.choice(win),self.n2)
print(random.choice(los),self.n1)
# Initiate your intro
game = Hero()
# Use the variable else where e.g. chance game
game.chance_game()
如果输入为空,则stringValue.split()返回False。原始条件不会包含双倍空格;)