下面的代码是我的课程中使用的代码。目前,“如果int(what_skill)<0或int(what_skill)> 9:”行将产生上述错误。我尝试将其更改为str(),但这会产生另一个错误。
def player():
vigor = 1
endurance = 1
strength = 1
dexterity = 1
intelligence = 1
luck = 1
points = 25
while points !=0:
what_skill = input("What skill would you like to add to? Vigor, Endurance, Strength, Dexterity, Intelligance, Luck? ")
print(what_skill)
add_points = int(input("You have " + str(points) + " points left. How many points from 1-9 would you like to add to " + str(what_skill) + "? "))
if int(add_points) > 9:
print("Too many points")
print(add_points)
else:
if int(what_skill) < 0 or int(what_skill) > 9:
print("invaid choice")
else:
update_skill = int(what_skill) + int(add_points)
points = points - add_points
您看到错误的原因是由于线条
int(what_skill)
在您的示例中,这等于
int("luck")
这没有任何意义-我想您正在尝试int(luck)
-引用变量中的数字
我会将您的技能更改为词典,并使用以下内容:
stats = {
"Luck": 1,
"Vigor": 1,
"Endurance": 1,
"Strength": 1,
"Dexterity": 1,
"Intelligence" : 1
}
points = 25
while points >= 1:
what_skill = input("What skill would you like to add to? Vigor, Endurance, Strength, Dexterity, Intelligance, Luck? ")
add_points = int(input("You have " + str(points) + " points left. How many points from 1-9 would you like to add to " + str(what_skill) + "? "))
if int(add_points) > 9:
print("Too many points")
else:
if stats[what_skill] < 0 or stats[what_skill] > 9:
print("invalid choice")
else:
update_skill = stats[what_skill] + int(add_points)
points = points - add_points
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句