2017-06-05 42 views
1

Hy!我正在創建一個計算概率的軟件。 我的問題是,當我想問我們在談論什麼類型的項目時,我寫它,我得到語法錯誤。嘗試在Python中讀取用戶輸入時的NameError

# -*- coding: utf-8 -*- 
from __future__ import division 
from scipy.stats import hypergeom 
import matplotlib.pyplot as plt 
from operator import itemgetter 
from random import randint 
import scipy.special 
import math 
import random 
from scipy.stats import binom 

object = str(input("What kind of items are we talking about?")) 
population = int(input("How many " + object + "do we have?")) 

錯誤:

What kind of items are we talking about?car 
Traceback (most recent call last): 
    File "/Users/suhajdakrisztian/PycharmProjects/firsttrial/input.nemtudomanevet.py", line 12, in <module> 
    object = str(input("What kind of items are we talking about?")) 
    File "<string>", line 1, in <module> 
NameError: name 'car' is not defined 

Process finished with exit code 1 
+1

正如溼婆的回答所示,默認情況下,輸入是一個字符串,因此您不需要在第一個輸入上調用str()。 – Actually

+1

順便說一句,不要使用'object'作爲變量名稱,因爲它會隱藏內置'object'類型。 –

回答

2

如果您使用python2,你要使用raw_input功能。 input功能將嘗試eval用戶輸入,這是你不想要的。

object = raw_input("What kind of items are we talking about?") 
population = int(raw_input("How many " + object + "do we have?"))