你好,我试图用python编写一个程序,要求用户输入一组1和0的数字,我希望程序告诉我,如果我有偶数个零,或奇数个零,或根本没有零。谢谢你的帮助!!在
forstate = "start"
curstate = "start"
trans = "none"
value = 0
print "Former state....:", forstate
print "Transition....:", trans
print "Current state....", curstate
while curstate != "You hav and even number of zeros":
trans = raw_input("Input a 1 or a 0: ")
if trans == "0" and value <2:
value = value + 1
forstate = curstate
elif trans == "1" and value < 2:
value = value + 0
forstate = curstate
curstate = str(value) + " zeros"
if value >= 2:
curstate = "You have and even number of zeros"
print "former state ...:", forstate
print "Transition .....:", trans
print "Current state....", curstate
听起来像是家庭作业,或者更糟的是面试问题,但这会让你开始。在
别忘了这部分
^{pr2}$如果你打电话给家庭作业并给它一个数字串,它会给你回0的数字
现在你知道了有多少个0需要用这个号码来打奇数或偶数
所以解决方案是这样的
看起来你在做一个有限状态机?在
编辑:
^{pr2}$相关问题 更多 >
编程相关推荐