Python SyntaxError:解析时出现意外的 EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16327405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:18:14  来源:igfitidea点击:

SyntaxError: unexpected EOF while parsing

pythoneclipsepython-3.xpydevpython-3.3

提问by Quar

I have no idea why this does not work please help

我不知道为什么这不起作用请帮忙

import random
x = 0
z = input('?')
int(z)

def main():
    while x < z:
        n1 = random.randrange(1,3)
        n2 = random.randrange(1,3)
        t1 = n1+n2
        print('{0}+{1}={2}'.format(n1,n2,t1)

When i run this it outputs this error

当我运行它时它输出这个错误

File "/Users/macbook/Documents/workspace/gamlir_filar/samlagning.py", line 12

                                                ^
SyntaxError: unexpected EOF while parsing

I am using eclipse and python 3.3 and i have no idea why this happens. It sometimes outputs errors like this.

我正在使用 eclipse 和 python 3.3,我不知道为什么会发生这种情况。它有时会输出这样的错误。

回答by Ashwini Chaudhary

You're missing a closing parenthesis )in print():

你缺少一个右括号)print()

print('{0}+{1}={2}'.format(n1,n2,t1))

and you're also not storing the returned value from int(), so zis still a string.

并且您也没有存储从 返回的值int(),所以z仍然是一个字符串。

z = input('?')
z = int(z)

or simply:

或者干脆:

z = int(input('?'))

回答by Kaydell

Maybe this is what you mean to do:

也许这就是你的意思:

import random

x = 0
z = input('Please Enter an integer: ')
z = int(z) # you need to capture the result of the expressioin: int(z) and assign it backk to z

def main():
    for i in range(x,z):
        n1 = random.randrange(1,3)
        n2 = random.randrange(1,3)
        t1 = n1+n2
        print('{0}+{1}={2}'.format(n1,n2,t1))

main()
  1. do z = int(z)
  2. Add the missing closing parenthesis on the last line of code in your listing.
  3. And have a for-loop that will iterate from x to z-1
  1. 做 z = int(z)
  2. 在清单的最后一行代码中添加缺少的右括号。
  3. 并有一个从 x 迭代到 z-1 的 for 循环

Here's a link on the range() function: http://docs.python.org/release/1.5.1p1/tut/range.html

这是 range() 函数的链接:http: //docs.python.org/release/1.5.1p1/tut/range.html