Python 如何处理语法错误?
对于我正在做的程序,它通过一个调查式的问题生成一个动漫推荐列表,并有一个CSV文件存储有关动漫的信息,如流派 但是,在运行程序时,我想测试在执行问题时是否会出现语法错误,并在执行所有问题后打印错误。我希望它能做的是,当输入错误时,语法错误会弹出 这就是我所做的:Python 如何处理语法错误?,python,loops,if-statement,Python,Loops,If Statement,对于我正在做的程序,它通过一个调查式的问题生成一个动漫推荐列表,并有一个CSV文件存储有关动漫的信息,如流派 但是,在运行程序时,我想测试在执行问题时是否会出现语法错误,并在执行所有问题后打印错误。我希望它能做的是,当输入错误时,语法错误会弹出 这就是我所做的: if adventure == 'yes': with open("AnimeGenre3.csv", 'r', newline='') as f: for row in csv.reader(f
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,语法错误的东西会弹出,然后重新提问。但在完成所有问题并生成动画列表后,它就会弹出
另一个问题是,在完成问题并打印推荐的动画后,我如何再次启动该程序。它会问你是否想重新开始,但它最终没有这样做。这就是我所做的
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.
现在,这是我到目前为止所做的全部程序,我真的需要找到一种使它更简单的方法,希望我的注释足够好
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.
关于重新开始,您唯一的代码将打印一条语句。它不重复一个功能。您可能需要考虑进一步调整(组织)程序,以便在重新启动时能够调用函数。例如:
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
就确保输入格式正确而言,这可能是另一个功能。它只需检查答案是“是”还是“否”,如果不是,则更改一个变量(如BADANSTALL==false变为true)。然后,当你想要的时候,它会触发其他的事情(重做)
我建议查看函数组织的“艰苦学习Python”(后面的章节),一些PyGame教程也有助于了解如何更好地调整编程(因为如果制作游戏,您需要一个整体函数来运行游戏,然后需要一组较小的函数来处理游戏中的活动).要么你在这个问题上的第一句话是谎言,要么你在8月31日的问题上的第一句话是谎言。不必要的内容是不被鼓励的,我不知道当这是一个谎言时该说什么——老实说,我没有太多编程经验@sааааааааааааааааӽ那么你应该先学习编程。这样的捷径真的不会带你去任何地方。我只是想知道我做错了什么@在我的程序中,我应该把“def main()”和“def animeselector()”放在哪里?看看我推荐的资源。并没有一个正确的方法,但main只会处理start、repeat、call其他函数,end。animeselector处理大部分真实逻辑,除非您可能分离出输入检查器。