如果在for循环python中跳过一些

时间:2013-11-21 18:35:13

标签: python if-statement for-loop

我差不多完成了游戏“热潮”。 用户输入2个数字(X,Y),在数字之间的范围和跳转(COUNTFROM,JUMP,COUNTUNTIL)和最后2个for循环之后,它应该打印(按照原始方式而不是列)如代码中所述(例如-x = 3因此代替6将打印“繁荣”) 问题是id不适用于所有if条件。 它仅适用于“Trach”并跳过其他人。 最后它应该打印一个原始数字和“繁荣”,“trach”。 致谢

import sys
Z = str(raw_input())
Digits_List = Z.split(" ")
X = int(Digits_List[0])
Y = int(Digits_List[1])
CountFrom = int(Digits_List[2])
jump = int(Digits_List[3])
CountUntil = int(Digits_List[4])

// some basic conditions
if (X<1 or X>9) or (Y<1 or Y>9):
    print "X and Y must be between 1 and 9"

if (jump==0):
    print "Cannot jump from ", CountFrom, " to ", CountUntil

if (CountFrom>0 and CountUntil>0 and jump<0):
    print "Cannot jump from ", CountFrom, " to ", CountUntil

if (CountFrom>0 and CountUntil<0 and jump>0):
    print "Cannot jump from ", CountFrom, " to ", CountUntil

if (CountFrom<0 and CountUntil>0 and jump<0):
    print "Cannot jump from ", CountFrom, " to ", CountUntil

if (CountFrom<0 and CountUntil<0 and jump>0):
    print "Cannot jump from ", CountFrom, " to ", CountUntil

portion = CountFrom - CountUntil

if (portion % jump != 0):
    print "Cannot jump from ", CountFrom, " to ", CountUntil

elements = []
for i in range(CountFrom, CountUntil+jump, jump):
    elements.append(i)

//2 FOR LOOPS- if-s dont work
for num in elements:    
    for num1 in str(num):
        if num1==X and num1==Y:
            print "BOOM-TRACH"
        elif num1==X:
            print "BOOM"
        elif num1== Y:
            print "TRACH"
    elif (num%X==0) and (num%Y==0):
        print "BOOM-TRACH"       
    elif (num%X == 0):
        print "BOOM"        
    elif (num%Y == 0):
        print "TRACH"
    else:
        print num

1 个答案:

答案 0 :(得分:0)

if num1==X and num1==Y:

此处num1是一个字符串。 XY是整数。他们永远不会平等。将num1转换为整数。