ST = 5
statAdd = 5
我输入的内容更多,但没有一个是相关的,所以我只是复制了它。
while statAdd > 0:
addStat = raw_input("""You may distribute %s points to your base stats. Which do you add to?
""" %(statAdd))
if addStat == 'strength':
pointDeduction = raw_input("How many points do you wish to add to Strength? (Up to %s points)" %(statAdd))
if pointDeduction <= statAdd:
ST += pointDeduction
statAdd -= pointDeduction
else:
print "You do not have that many points to distribute to Strength."
你会认为它应该添加积分,但我一直得到同样的错误&#34;你没有那么多积分来分配力量&#34;当我明显这样做的时候我在这里做错了什么?
答案 0 :(得分:1)
尝试将输入转换为int
?否则它是一个字符串,对它进行算术运算会导致意想不到的结果。
while statAdd > 0:
addStat = int(raw_input("""You may distribute %s points to your base stats. Which do you add to? """ %(statAdd)))
if addStat == 'strength':
pointDeduction = int(raw_input("How many points do you wish to add to Strength? (Up to %s points)" %(statAdd)))
if pointDeduction <= statAdd:
ST += pointDeduction
statAdd -= pointDeduction
else:
print "You do not have that many points to distribute to Strength."
答案 1 :(得分:0)
raw_input
返回一个字符串。
如果存在prompt参数,则将其写入标准输出而不带尾随换行符。然后该函数从输入中读取一行,将其转换为字符串(剥离尾随换行符),然后返回该行。
您的if pointDeduction <= statAdd:
语句正在进行此比较:
"5" <= 5
这将返回False
要解决此问题,您需要使用int
包装输入语句(如果您只允许整数)。
addStat = int(raw_input("""You may distribute %s points to your base stats. Which do you add to? """ %(statAdd)))
答案 2 :(得分:0)
在python 2.x中,raw_input()
返回一个字符串,因此在其上执行的操作就像不能工作一样。你应该使用普通input()
代替(在第二个),它在Python 2.x中自动对输入执行eval:
while statAdd > 0:
addStat = raw_input("""You may distribute %s points to your base stats. Which do you add to?""" %(statAdd))
if addStat == 'strength':
pointDeduction = input("How many points do you wish to add to Strength? (Up to %s points)" %(statAdd))
if pointDeduction <= statAdd:
ST += pointDeduction
statAdd -= pointDeduction
else:
print "You do not have that many points to distribute to Strength."
这应该可行。注意,在Python 3.x中,input()返回一个字符串,而eval(input())将返回int。