首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理语法错误?

如何处理语法错误?
EN

Stack Overflow用户
提问于 2020-09-13 07:51:22
回答 1查看 59关注 0票数 1

对于我正在做的程序,它通过调查风格问题生成一个动漫推荐列表,并有一个CSV文件存储有关动漫的信息,如流派。

然而,在运行程序时,我想测试做问题时是否会出现语法错误,并在做完所有问题后打印错误。我希望它能做的是,当输入不正确时,会弹出语法错误。

这就是我所做的:

代码语言:javascript
复制
if adventure == 'yes':
   with open("AnimeGenre3.csv", 'r', newline='') as f:
       for row in csv.reader(f):
            if row[4] == 'yes':
               anime = str(row[1])
               looping = False
               if anime not in anime_list:
                  anime_list.append(anime)

else:
    print('''
There seems to be a problem in one of the questions
    ''')

比方说,“你喜欢动漫吗”,然后你输入6,语法错误的东西就会弹出来,并重新做这个问题。但在做完所有问题并生成动漫列表后,它就会弹出。

另一个问题是如何在完成问题并打印推荐的动漫后再次启动程序。它会询问您是否要重新开始,但最终没有这样做。这就是我所做的。

代码语言:javascript
复制
StartAgain = str(input('Do you like to start again; yes or no: '))

if StartAgain == 'yes':
     print('''
Let's begin ''')
break
if StartAgain == 'no':
      print('thank you for coming')

else:
  print('''
 Sorry Man got to do it again''')

 # an if statement if the user wants to start again
 # this should rerun the program.

现在,这是我到目前为止完成的完整程序,老实说,我需要找到一种方法来让它变得更简单,希望我的注释足够好。

代码语言:javascript
复制
 import csv
 anime_list = []
 #in the csv AnimeGenre3.csv it contains information such as the animes, the number of 
 episodes and genres

#printing information about the program.
#has an if statement to identify if it user wants to start the program.

looping1 = True
looping2 = True

print('This is a program that identifies specific anime for you')

while looping1 == True:
begin = str(input('Do you like to start; yes or no: '))

if begin == 'yes':
    print('''
Let's begin
''')
    break
if begin == 'no':
    print('thank you for coming')
else:
    print('''
Sorry Man got to do it again
        ''')
#this section is where the program ask the user questions from there
# it will identify, the genres the user is interested in.

while looping2 == True:

action = str(input('Do you like action; yes or no: '))
adventure = str(input('Do you like adventure; yes or no: '))
print('')
print('recommended anime')
#having to use if statements to find any anime that either say "yes" in the CSV file
# it sorts out all of the sorts that the user said yes to.
# It goes through the rows to identify all of the user genres that say yes
# it should print out the recommend anime
# this prints out a long list of anime

if action == 'yes':
    with open("AnimeGenre3.csv", 'r', newline='') as f:
        for row in csv.reader(f):
            if row[3] == 'yes':
                anime = str(row[1])
                looping = False
                if anime not in anime_list:
                    anime_list.append(anime)
                    
if adventure == 'yes':
    with open("AnimeGenre3.csv", 'r', newline='') as f:
        for row in csv.reader(f):
            if row[4] == 'yes':
                anime = str(row[1])
                looping = False
                if anime not in anime_list:
                    anime_list.append(anime)

else:
    print('''
There seems to be a problem in one of the questions
        ''')
# if there is a syntax error within the program, it should pop up to the user saying this
    
print(anime_list)

StartAgain = str(input('Do you like to start again; yes or no: '))

if StartAgain == 'yes':
    print('''
Let's begin ''')
    break
if StartAgain == 'no':
    print('thank you for coming')

else:
    print('''
Sorry Man got to do it again''')

# an if statement if the user wants to start again
# this should rerun the program.
EN

回答 1

Stack Overflow用户

发布于 2020-09-13 08:32:51

关于重新启动,您唯一的代码将打印一条语句。它不会重复一个函数。您可能希望考虑对您的程序进行更多的调制(组织),这样当您再次启动时,您可以调用某个函数。例如,

代码语言:javascript
复制
def main()
      //this contains your overall logic, starting/ending the program and/or repeating it
     // at some point in time, you call animeselector() to start, and if it needs repeating, you call it again

 def animeselector ()
      //this controls the in/out and selection process

就确保输入格式正确而言,这可能是另一个功能。它可以只检查答案是“是”还是“否”,如果不是,则更改一个变量(如badanswer == false变为true)。然后,这会在你想要的时候触发其他事情(重做)。

我建议你去看看学习Python的方法(后面的章节),一些PyGame教程也可以很好地了解如何更好地调整你的编程(因为如果制作一款游戏,你需要一个整体的函数来运行游戏,而不是一堆更小的函数来处理游戏中的活动)。

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

https://stackoverflow.com/questions/63865904

复制
相关文章

相似问题

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