我不明白的错误
Errors that I don't understand
我正在尝试根据以下教程编写代码:
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 什么都不做。
您必须在 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()
。
在 while
循环中的 print
语句之前有 4 spaces,但该循环中的所有其他行只有 3 spaces 缩进,从 selection = raw_input...
您应该在 selection = raw_input...
及以下的每一行的开头添加一个 space。
我正在尝试根据以下教程编写代码: 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 什么都不做。
您必须在 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()
。
在 while
循环中的 print
语句之前有 4 spaces,但该循环中的所有其他行只有 3 spaces 缩进,从 selection = raw_input...
您应该在 selection = raw_input...
及以下的每一行的开头添加一个 space。