2016-06-10 11 views
5

के बीच अंतर मैंने पाइथन कोड के नीचे लिखा था। और मैंने पाया कि Python2 और python3 1.1 के इनपुट के लिए पूरी तरह से अंतर चल रहा है। पायथन 2 और पायथन 3 के बीच ऐसा अंतर क्यों है? मेरे लिए, int (1.1) 1 होना चाहिए, तो स्थिति 0,1,2 के भीतर वैध अनुक्रमणिका 1 है। तो क्या आप कृपया बता सकते हैं कि python3 का ऐसा परिणाम क्यों है?पायथन 2 और पायथन 3 - int() और इनपुट()

s=[1,2,3] 
while True: 
    value=input() 
    print('value:',value) 
    try: 
    position=int(value) 
    print('position',position) 
    print('result',s[position]) 
    except IndexError as err: 
    print('out of index') 
    except Exception as other: 
    print('sth else broke',other) 


$ python temp.py 
1.1 
('value:', 1.1) 
('position', 1) 
('result', 2) 


$ python3 temp.py 
1.1 
value: 1.1 
sth else broke invalid literal for int() with base 10: '1.1' 
+0

इसे वास्तव में काम करने के लिए, आप स्थिति = int (float (value)) कर सकते हैं –

+1

क्या आप मूल्य के प्रकार की पुष्टि करने का प्रयास कर सकते हैं? –

उत्तर

4

मुद्दा intput() है को Python2 के लिए एक नंबर और अजगर के लिए एक स्ट्रिंग 3.

एक गैर पूर्णांक स्ट्रिंग के int() एक त्रुटि देता है के लिए मूल्य बदल देता है, जबकि पूर्णांक (एक नाव की) नहीं करता है।

value=float(input()) 

या, बेहतर (सुरक्षित) अभी तक

position=int(float(value)) 

संपादित करें:

या तो का उपयोग कर एक नाव के लिए इनपुट मूल्य कन्वर्ट और सबसे अच्छी बात है, क्योंकि यह एक eval का उपयोग करता input उपयोग करने से बचें और असुरक्षित है। के रूप में Tadhg का सुझाव दिया, सबसे अच्छा समाधान है:

#At the top: 
try: 
    #in python 2 raw_input exists, so use that 
    input = raw_input 
except NameError: 
    #in python 3 we hit this case and input is already raw_input 
    pass 

... 
    try: 
     #then inside your try block, convert the string input to an number(float) before going to an int 
     position = int(float(value)) 

अजगर डॉक्स से:

PEP 3111: raw_input() was renamed to input() . That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input() , use eval(input()) .

+3

यहां तक ​​कि सुरक्षित होगा 'कोशिश करें: इनपुट = raw_input; NameError को छोड़कर: पास करें और फिर इनपुट को दोनों परिदृश्यों में एक स्ट्रिंग के रूप में देखें। –

1

अजगर 3 रिलीज नोट्स की जांच करें। विशेष रूप से, input() फ़ंक्शन (जिसे खतरनाक माना जाता है) हटा दिया गया था। इसके स्थिर में, सुरक्षित raw_input() फ़ंक्शन का नाम बदलकर input() कर दिया गया था।

दोनों संस्करणों के लिए कोड लिखने के लिए, केवल raw_input() पर भरोसा करें। अपनी फ़ाइल के शीर्ष पर निम्नलिखित जोड़ें:

try: 
    # replace unsafe input() function 
    input = raw_input 
except NameError: 
    # raw_input doesn't exist, the code is probably 
    # running on Python 3 where input() is safe 
    pass 

बीटीडब्ल्यू: आपका उदाहरण कोड न्यूनतम नहीं है। यदि आपने कोड को और कम कर दिया है, तो आप पाएंगे कि एक मामले में int()float पर और दूसरे में str पर चलता है जो आपको input() रिटर्न पर अलग-अलग चीज़ों पर लाएगा। दस्तावेज़ों पर एक नज़र ने आपको अंतिम संकेत दिया होगा।

संबंधित मुद्दे