Python 2.7程序无法在MAC终端中运行

时间:2014-06-18 15:31:27

标签: python macos python-2.7 terminal

我正在为学生讲授编码课程,我们正在尝试运行以下课程。它给出了语法错误。以下是程序和错误消息:

# Introduction
print 'Hello. If you give me your scores, I can find your overall grade.'
ques = raw_input('Would you like me to find your class grade? y/n ')


# Get first score
if ques in ['y', 'Y', 'yes', 'Yes']:
    count = 1
    sum = int(raw_input('What is your first score? ')
    ans = raw_input('Do you have another score for me? y/n ')
else:
    ans = 'no'
    print 'Thanks anyway. Run me later if you change your mind.'

# Get other scores
while ans in ['y', 'Y', 'yes', 'Yes']:
    sum = sum + int(raw_input('What is the next score? ')
    count = count + 1
    ans = raw_input('Do you have another score for me? y/n ')

# Calculate score
avg = sum / count
print 'Your class grade is:', avg

错误讯息:

Last login: Wed Jun 18 10:07:31 on ttys000
D-iMac-00:~ prog$ /var/folders/1r/zxdwc24s2h5gncz_q09x46rw0000gq/T/Cleanup\ At\ Startup/fancy_grade-424798099.021.py.command ; exit;
  File "/Users/prog/Desktop/Jake/fancy_grade.py", line 10
    ans = raw_input('Do you have another score for me? y/n ')
      ^
SyntaxError: invalid syntax
logout

[Process completed]

2 个答案:

答案 0 :(得分:1)

您缺少前一行的右括号:

sum = int(raw_input('What is the next score? ')
#        ^         ^                          ^^ ?
#        |          \-------- matched -------/ |
#         \-------------- missing --------------/

您稍后会在该计划中再次这样做:

sum = sum + int(raw_input('What is the next score? ')
#              ^         ^                          ^^ ?
#              |          \-------- matched -------/ |
#              \-------------- missing --------------/

您可能需要查看Asking the user for input until they give a valid response,了解有关如何更好地处理用户输入的提示。

答案 1 :(得分:0)

你在第9行缺少一个“)”。语法错误通常与那样的事情有关。