【发布时间】:2015-03-21 16:26:46
【问题描述】:
这里是初学者,
我正在编写一个为用户掷骰子的程序,我希望它能够根据用户输入更改骰子的面数。我似乎无法让变量 amount_faces 作为 randint() 函数的 int 工作,每次都会出现“TypeError: cannot concatenate 'str' and 'int' objetcts”错误:
from sys import exit
from random import randint
def start():
print "Would you like to roll a dice?"
choice = raw_input(">")
if "yes" in choice:
roll()
elif "no" in choice:
exit()
else:
print "I can't understand that, try again."
start()
def roll():
print "How many faces does the die have?"
amount_faces = raw_input(">")
if amount_faces is int:
print "The number of faces has to be an integer, try again."
roll()
else:
print "Rolling die...."
int(amount_faces)
face = randint(1,*amount_faces)
print "You have rolled %s" % face
exit()
start()
有什么线索吗?
【问题讨论】:
标签: python string int concatenation