因此,我尝试从视频中创建自己的示例,以便更好地理解示例和课程。在构建我自己的过程中,我遇到了两个错误。第一个是:
status = input('Are you single or married?: '.lower())
File "<string>", line 1, in <module>
NameError: name 'single' is not defined
第二个是:
except ValueError:
^
SyntaxError: invalid syntax
我的代码是:
print ('When entering numbers, do not use commas or periods''\n')
salary = int(input('What is your annual salary before taxes?'))
status = input('Are you single or married?: '.lower())
if status == ('single'):
status = (status)
elif status == 'married':
status = ('married')
except ValueError:
print('Sorry I did not understand your input')
else:
print('You answered that you are {} making {} a year').format(status, salary)
我看过这些视频重新阅读了我的笔记,但我想是因为我正在尝试构建我自己的例子,我错过了一些不在视频中的简单。
答案 0 :(得分:1)
你的第一个错误,那是因为你正在使用python 2.你应该使用raw_input()
而不是input()
。 input()
仅适用于数字输入。
关于您的SyntaxError
,这是因为您之前没有except
声明try
,我认为您正在寻找else
声明。
以下是您的代码的修改版本:
print ('When entering numbers, do not use commas or periods''\n')
salary = input('What is your annual salary before taxes?')
status = raw_input('Are you single or married?: ').lower()
# put .lower() outside if you want the input to be lowercase instead of the question with lowercase letters.
if status != 'single' and status != 'married':
print('Sorry I did not understand your input')
else:
print('You answered that you are {} making {} a year').format(status, salary)
(我没有测试过,但我希望它有效)