我正在尝试从以下教程中编写代码: https://www.youtube.com/watch?v=9mAmZIRfJBs&t=197s 在我看来,我完全是用相同的方式编写的,但是仍然会出现错误。有人可以向我解释为什么Spyder(Python 3.7)会这样做。 这是我的代码:
我尝试使用另一个输入函数,所以用raw_input代替了输入。我还尝试过更改工作目录并保存文档
这是我的代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 29 14:47:27 2019
@author: johan
"""
import random
restaurantsList = ['boloco', 'clover', 'sweetgreens']
def pickRestaurant():
print(restaurantsList[random.randint(0,2)])
def addRestaurant(name):
restaurantsList.append(name)
def removeRestaurant(name):
restaurantsList.remove(name)
def listRestaurant():
for restaurant in restaurantsList:
print(restaurant)
while True:
print('''
[1] - List restaurant
[2] - Add restaurant
[3] - Remove restaurant
[4] - Pick restaurant
[5] - Exit
''')
selection = raw_input(prompt='Please select an option: ')
if selection == '1':
print('')
listRestaurant()
elif selection == '2':
inName = raw_input(prompt='Type name of the restaurant that you want to add: ')
addRestaurant(inName)
elif selection == '3':
inName = raw_input(prompt='Type name of the restaurant that you want to remove: ')
removeRestaurant(inName)
elif selection == '4':
pickRestaurant()
elif selection == '5':
break
这是错误
runfile('C:/Users/johan/Desktop/Unie jaar 2/untitled2.py', wdir='C:/Users/johan/Desktop/Unie jaar 2')
Traceback (most recent call last):
File "C:\Users\johan\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-93-2d7193d6cafb>", line 1, in <module>
runfile('C:/Users/johan/Desktop/Unie jaar 2/untitled2.py', wdir='C:/Users/johan/Desktop/Unie jaar 2')
File "C:\Users\johan\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\johan\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/johan/Desktop/Unie jaar 2/untitled2.py", line 35
selection = raw_input(prompt='Please select an option: ')
^
IndentationError: unindent does not match any outer indentation level
应该给餐厅清单的代码是放置1个餐厅。如果放置2个餐厅,则可以将餐厅添加到列表中。想要添加3个餐厅,然后将其删除。 4从列表中选择一个随机餐厅。 5什么也不做。
答案 0 :(得分:0)
因此必须在Python中正确缩进;
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 29 14:47:27 2019
@author: johan
"""
import random
restaurantsList = ['boloco', 'clover', 'sweetgreens']
def pickRestaurant():
print(restaurantsList[random.randint(0,2)])
def addRestaurant(name):
restaurantsList.append(name)
def removeRestaurant(name):
restaurantsList.remove(name)
def listRestaurant():
for restaurant in restaurantsList:
print(restaurant)
while True:
print('''
[1] - List restaurant
[2] - Add restaurant
[3] - Remove restaurant
[4] - Pick restaurant
[5] - Exit
''')
selection = input('Please select an option: ')
if selection == '1':
print('')
listRestaurant()
elif selection == '2':
inName = input('Type name of the restaurant that you want to add: ')
addRestaurant(inName)
elif selection == '3':
inName = input('Type name of the restaurant that you want to remove: ')
removeRestaurant(inName)
elif selection == '4':
pickRestaurant()
elif selection == '5':
break
Python是缩进敏感的,创建函数或任何语句时,您需要缩进该函数内的任何代码,否则您将得到上面的错误。
附加说明:您使用的是print()
(它是python2)和raw_input
(它是python3),所以我假设使用Python3,并将raw_input()
更改为input()
。 / p>
答案 1 :(得分:0)
print
循环中的while
语句之前有4个空格,但是该循环中的所有其他行仅从selection = raw_input...
开始有3个空格缩进
您应该在selection = raw_input...
及以下的每一行的开头添加一个空格。