[英]EOFError due to prompting the user input in python 2
我正在嘗試使用 Python 2.7 執行上述代碼。 我得到EOFError
。
def perfectNumberCheck(num):
sum = 0
for i in range(1, num):
if num % i == 0:
sum = sum + i
if sum == num:
print('1')
else:
print('0')
num = int(raw_input('come on dude:'))
perfectNumberCheck(num)
我在 Python 3 中沒有問題,但需要在 Python 2 中提交作業。
當您的操作系統在后台自動調用腳本時,腳本中沒有 STDIN,因此它收到一個意外的文件結尾(因為,從技術上講, stdin=/dev/null ,它總是給出 eof)。 如此處所述,您可以嘗試以下部分:
import sys
for line in sys.stdin:
print line
sys.stdin = open('CON', 'r')
num = raw_input("come on dude:")
perectNumberCheck(num)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.