【问题标题】:Python 2.7 Program not working in MAC terminal [closed]Python 2.7程序在MAC终端中不起作用[关闭]
【发布时间】:2014-06-18 15:31:27
【问题描述】:

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

# 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]

【问题讨论】:

    标签: python macos python-2.7 terminal


    【解决方案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,了解如何更好地处理用户输入的提示。

    【讨论】:

    • 完美。我没有仔细看就复制粘贴了。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-10-23
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2013-09-25
      • 1970-01-01
      • 2022-11-13
      相关资源
      最近更新 更多