Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected result
#1
Os-Ubuntu 18.04 Python - Ver 3.6 Using Idle .

dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ')
print(dms[0])
Output:
Output:
=============== RESTART: /home/linton/Documents/Python/test.py =============== DR_Lat (deg,min,sec,N/S/E/W) = 54,20,20 5
My expectation from the internet searches I have done is that the output for the index dms[0] should be the element before the first comma - in this case 54 but it only outputs the first digit 5 and not the whole indexed element.
Reply
#2
Of course it does - each position in a string is a single character. Where did you find otherwise? You can at least Do something like finding the position of the first comma and then use slicing to get everything from the beginning up to (but excluding) that.
Reply
#3
your expectation would be true, if dms was list, but it is not. It is a str.
you need to split the str returned by input(). Note that dms[0] will still be str.
dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ').split(',')
print(dms[0])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
dms holds string. Hence dms[0] gives first char which is expected behaviour.

Use split() as here to convert input to list

>>> dms = input('DR_Lat (deg,min,sec,N/S/E/W) = ')
DR_Lat (deg,min,sec,N/S/E/W) = 54,20,20
>>> dms
'54,20,20'
>>> type(dms)
<class 'str'>
>>> dms[0]
'5'
>>> dmsLst = input('DR_Lat (deg,min,sec,N/S/E/W) = ').split(",")
DR_Lat (deg,min,sec,N/S/E/W) = 54,20,20
>>> type(dmsLst)
<class 'list'>
>>> dmsLst[0]
'54'
Reply
#5
Thanks buran, anbu23 - got it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list sum gives unexpected result Nesso 0 539 Feb-04-2020, 08:31 AM
Last Post: Nesso
  Unexpected (?) result with regular expressions guraknugen 2 693 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Unexpected expected type error result MartinMaker 1 1,010 Feb-16-2019, 05:02 PM
Last Post: micseydel
  unexpected sub result after overloading operator jolinchewjb 1 1,028 Jan-24-2019, 08:23 AM
Last Post: buran
  Unexpected result eftimios 1 1,272 Dec-02-2018, 07:39 AM
Last Post: Gribouillis
  Unexpected result in simple prime number example jackhj 2 1,519 Apr-20-2018, 01:48 AM
Last Post: jackhj
  Reversing word in strings yields unexpected result Dec 4 2,044 May-17-2017, 05:32 PM
Last Post: wavic
  datetime unexpected result PickyBiker 10 5,543 Dec-27-2016, 10:47 PM
Last Post: PickyBiker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020