【发布时间】:2012-09-28 14:33:17
【问题描述】:
我正在尝试制作一个非常简单的 python 脚本,将两个字符放在一起,但是在运行脚本时,它执行脚本用来定义两个字符统计信息的前两个函数,但是当它到达第三个函数时,它只是挂起。
代码如下:
#STAPS: Strength Toughness Agility Perception weapon Skill
#A comparative simulator
import random
#Functions used to define character parameters
#Character 1's parameter function
def char1():
global s1
global t1
global a1
global p1
global dam1
global dt1
global dr1
global ac1
global ws1
s1 = int(input("Char1's Strength? "))
t1 = int(input("Char1's Toughness? "))
a1 = int(input("Char1's Agility? "))
p1 = int(input("Char1's Perception? "))
dam1 = int(input("Char1's Damage? "))
dt1 = int(input("Char1's Damage Threshold? "))
dr1 = int(input("Char1's Damage Resistance? "))
ac1 = int(input("Char1's Armor Class? "))
ws1 = int(input("Char1's Weapon Skill? "))
#Character 2's paramter function
def char2():
global s2
global t2
global a2
global p2
global dam2
global dt2
global dr2
global ac2
global ws2
s2 = int(input("Char2's Strength? "))
t2 = int(input("Char2's Toughness? "))
a2 = int(input("Char2's Agility? "))
p2 = int(input("Char2's Perception? "))
dam2 = int(input("Char2's Damage? "))
dt2 = int(input("Char2's Damage Threshold? "))
dr2 = int(input("Char2's Damage Resistance? "))
ac2 = int(input("Char2's Armor Class? "))
ws2 = int(input("Char2's Weapon Skill? "))
#Main battle function. Ordo Xenos calls this "complex and easy to misuse"
#Jury-rigged way of getting names, why did I include them anyways?
def stapsbatt(c1n,c2n,hp1,hp2):
while hp1 > 0 or hp2 > 0:
#determines original raw acc
char1rawacc = ws1 - ac2
#if statement settles it to minimum 95% acc
if char1rawacc > 95:
char1rawacc = 95
#random int used to determine whether it's a hit or not
char1hitnum = random.randint(0, 100)
if char1rawacc > char1hitnum:
moddam1 = dam1 - dt2
if moddam1 < 0:
moddam1 = 0
rawdam1 = moddam1 * (100 - dr2)
hp2 = hp2 - rawdam1
#Now we move on to doing char2's batt calcs
char2rawacc = ws2 - ac1
if char2rawacc > 95:
char2rawacc = 95
char2hitnum = random.randint(0, 100)
if char2rawacc > char2hitnum:
moddam2 = dam2 - dt1
if moddam2 < 0:
moddam2 = 0
rawdam2 = moddam2 * (100 - dr1)
hp1 = hp1 - rawdam2
if hp1 == 0:
print(c2n, "has won!")
else:
print(c1n, "has won!")
char1()
char2()
stapsbatt("Character 1", "Character 2",400,30)
input("Press enter to exit. ")
是的,这段代码完全未经编辑,我意识到我的 cmets 不是很好。
【问题讨论】:
-
@Klikun,不要过度滥用全局变量。有更好的方法可以从不同的范围访问变量。
-
你说“是的,这段代码完全未经编辑”,那为什么不编辑呢?添加一些打印语句来确定代码挂起的位置,然后发布(事实上它与全局相关等)。 [短,独立,正确(可编译),示例(]sscce.org):)
-
@Oz123 对不起!我最近了解了函数,并且一直想将这种模拟从笔和纸转移到脚本,所以我一有机会就跳了起来。虽然仍在学习,但我刚刚了解了范围和非常基本的类。
-
@hayden 我已经知道挂起的位置,我知道它是在第三个函数的开头,我只是不知道是什么原因造成的。我所说的未经编辑的意思是我希望这是我尴尬的 cmets 的借口。
-
“一开始”相当模糊,打印语句可以让你在精确的行上磨练(实际上 CRTL+C 会停止该过程并告诉你最后运行的行):)
标签: python python-3.2