• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

adding an empty string?

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've been putzing around with Python (I previously took Java I and II in college) and am confused about something. This code is to turn a base 10 number into a binary one.
This is not my code. On line number 14 I don't understand what "+ result" is doing. result is defined as '', which I believe is just an empty string, yet if I comment out + result the answer
will be 1. If i keep + result the answer is correct at 111110100. I just don't understand what is taking place to have result be an empty string to just adding it coming up with the correct answer.

Thanks for any help.


 
author & internet detective
Posts: 41125
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
result is only an empty string the first time through the loop. The first time, you have
result = str(num%2) + result
result = '0' + ''
result = '0'

The second time through the loop, you have:
result = str(num%2) + result
result = '0' + '0'
result = '00'

And so forth.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic