为什么会出现这种结果

时间:2015-08-04 15:18:23

标签: python python-3.x

playerlevel = 1
playerattack = 10
playerhealth = 100
playerexp = 0

print (playerlevel)
answer = input('have you played before? yes or no: ')
if answer == 'yes':
    level = input('what was your level: ')
    playerlevel *= level
    playerattack *= level
playerhealth *= level
else:
    pass

print ("player level: %s" % playerlevel)
print ("player health: %s" % playerhealth)
print ("player attack: %s" % playerattack)
print ("player exp: %s" % playerexp)

当我运行这个程序时,它运行玩家等级如果等级为2它打印2但其他打印 - >每个222222222222222222222222222222222。为exp 0

1 个答案:

答案 0 :(得分:2)

level = input('what was your level: ')

input返回一个字符串*。将字符串乘以整数时,会多次重复该字符串。例如,"2" * 3"222"

如果你想要实际的数字乘法,在使用之前将等级转换为整数。

level = int(input('what was your level: '))

*仅适用于Python 3.X. input可以返回2.7及更低版本中的其他类型,但您应该在这些版本中使用raw_input