Ho scritto sotto il codice Python. E ho scoperto che python2 e python3 hanno totalmente differenza nel risultato in esecuzione per l'input di 1.1. Perché c'è una tale differenza tra python2 e python3? Per me, int (1.1) dovrebbe essere 1, quindi la posizione è indice valido 1 entro l'intervallo 0,1,2. Quindi, per favore, puoi spiegare perché python3 ha tale risultato?differenza tra python2 e python3 - int() e input()
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'
Per far sì che funzioni davvero, puoi fare position = int (float (valore)) –
Puoi provare a verificare il tipo di valore? –