首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >raw_input()无效语法

raw_input()无效语法
EN

Stack Overflow用户
提问于 2013-11-24 21:46:46
回答 1查看 2K关注 0票数 0

我想输入一个由五个数字组成的字符串,并在它们之间加上空格,并为此使用raw_input()。但是,第二个项目(第一个和第二个空格之间的部分)被声称是一个syntax error。代码如下:

代码语言:javascript
复制
#class for final output - used as an ad hoc static string
class StatString:
        outstring = ""

#function to check if Boom or Trach or both
def BoomTrach(num,B,T):
        Boom = False
        Trach = False
        temp = num
        while temp != 0:
                if num % B == 0:
                        Boom == True
                        break
                if (temp % 10) % B == 0:
                        Boom = True
                        break
                temp = (temp - temp % 10) / 10
                temp = num
        while temp != 0:
                if num % T == 0:
                        Trach = True
                        break
                if (temp % 10) % T == 0:
                        Trach = True
                        break
                temp = (temp - temp % 10) / 10
        if Boom and Trach:
                herestring.outstring = herestring.outstring + "Boom-Trach"
        elif Boom:
                herestring.outstring = herestring.outstring + "Boom"
        elif Trach:
                herestring.outstring = herestring.outstring + "Trach"
        else:
                herestring.outstring = herestring.outstring + str(num)


#start of "main" portion
def main():
        inS = raw_input()  <<<--- Input here
        arr = inS.split(' ')
        X = int(arr[0])
        Y = int(arr[1])
        CountFrom = int(arr[2])
        jump = int(arr[3])
        CountUntil = int(arr[4])

        #variable for error check
        error = False

        #checking for errors
        if X < 1 or X > 9 or Y < 1 or Y > 9:
                print "X and Y must be between 1 and 9"
                error = True

        if jump == 0:
                print "jump cannot be 0"
                error = True

        elif (CountUntil - CountFrom) % jump != 0:
                print "cannot jump from %d to %d",CountFrom,CountUntil
                error = True

        if error:
                exit()

        if CountFrom < 0 and CountUntil < 0 and jump > 0:
                jump = jump * (-1)

        herestring = StatString()
        while CountFrom != CountUntil:
                BoomTrach(CountFrom,X,Y)
                CountFrom = CountFrom + jump
                if(CountFrom != CountUntil):
                        herestring.outstring = herestring.outstring + ","
        print herestring.outstring

错误消息:(第二个1被标记为错误源)

代码语言:javascript
复制
>>> 1 1 1 1 1
SyntaxError: invalid syntax
>>> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-28 16:12:10

我知道发生了什么。您只需运行这个模块,就可以从主函数开始运行它(例如,在C中)。raw_input行(输入的第一行)没有问题。问题是您的模块没有尝试读取任何东西!你刚输入了“1111”,这当然是语法错误.

将以下代码附加到代码中以运行主函数:

main()

您也可以编写主函数的代码,而不是在某些函数中,以获得相同的效果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20181437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档