这个python程序有什么问题?它在询问第一个字符名称后挂起

时间:2014-01-10 12:36:26

标签: python function

好的,所以当程序询问第一个字符名称并输入时,它只是挂起而没有任何反应。如果有人能帮助我,我真的很感激!这是我的代码:

import math, random
name1 = ""
name2 = ""
strength1 = 0
strength2 = 0
skill1 = 0
skill2 = 0

def Character1(strength1, skill1, name1):
    name1 = raw_input("Please enter a name for the first character : ")
    strength1 = math.floor(random.randint(1,12)/random.randint(1,4))+10
    skill1 = math.floor(random.randint(1,12)/random.randint(1,4))+10

def Character2(strength2, skill2, name2):
    name2 = raw_input("Please enter a name for the second character : ")
    strength2 = math.floor(random.randint(1,12)/random.randint(1,4))+10
    skill2 = math.floor(random.randint(1,12)/random.randint(1,4))+10

def printStats(strength1, skill1, name1, strength2, skill2, name2):
    print "The first character is called " + name1 + " and has a skill level of " + str(skill1) + " and a strength level of " + str(strength1) 
    print "The first character is called " + name2 + " and has a skill level of " + str(skill2) + " and a strength level of " + str(strengths2) 

Character1(strength1, skill1, name1)
Character2(strength2, skill2, name2)
printStats(strength1, skill1, name1, strength2, skill2, name2)

P.s:我正在使用python 2.7

1 个答案:

答案 0 :(得分:0)

我怀疑你是否运行过你的代码。

首先,纠正代码中的拼写错误,例如strength2,skills1 ......

其次,如果您确实想要使用全局变量,请在函数内声明它们,否则对全局变量的赋值将使它们成为局部变量。以下是代码:

import math, random
name1 = ""
name2 = ""
strength1 = 0
strength2 = 0
skill1 = 0
skill2 = 0

def Character1():
    global strength1, skill1, name1
    name1 = raw_input("Please enter a name for the first character : ")
    strength1 = math.floor(random.randint(1,12)/random.randint(1,4))+10
    skill1 = math.floor(random.randint(1,12)/random.randint(1,4))+10

def Character2():
    global strength2, skill2, name2
    name2 = raw_input("Please enter a name for the second character : ")
    strength2 = math.floor(random.randint(1,12)/random.randint(1,4))+10
    skill2 = math.floor(random.randint(1,12)/random.randint(1,4))+10

def printStats():
    print "The first character is called " + name1 + " and has a skill level of " + str(skill1) + " and a strength level of " + str(strength1) 
    print "The first character is called " + name2 + " and has a skill level of " + str(skill2) + " and a strength level of " + str(strength2) 

Character1()
Character2()
printStats()