Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in integer from stdin in Python

I have the following piece of code where I take in an integer n from stdin, convert it to binary, reverse the binary string, then convert back to integer and output it.

import sys

def reversebinary():
  n = str(raw_input())
  bin_n = bin(n)[2:]
  revbin = "".join(list(reversed(bin_n)))
  return int(str(revbin),2)

reversebinary()

However, I'm getting this error:

Traceback (most recent call last):   
File "reversebinary.py", line 18, in <module>
  reversebinary()   
File "reversebinary.py", line 14, in reversebinary
   bin_n = bin(n)[2:] 
TypeError: 'str' object cannot be interpreted as an index

I'm unsure what the problem is.

like image 493
Phil Kurtis Avatar asked May 31 '13 23:05

Phil Kurtis


People also ask

How to read data from stdin in Python?

There are three ways to read data from stdin in Python. 1. Using sys.stdin to read from standard input Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input () function. The input string is appended with a newline character ( ) in the end. So, you can use the rstrip () function to remove it.

How to read standard input in infinite loop in Python?

We can also use Python input () function to read the standard input data. We can also prompt a message to the user. Here is a simple example to read and process the standard input message in the infinite loop, unless the user enters the Exit message.

How to remove newline character from standard input in Python?

Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input () function. The input string is appended with a newline character ( ) in the end. So, you can use the rstrip () function to remove it. Here is a simple program to read user messages from the standard input and process it.

How to use Sys in Python to take input?

To use sys in Python, we firstly import sys There are a number of ways in which we can take input from stdin in Python. Using sys.stdin: sys.stdin can be used to get input from the command line directly. It used is for standard input.


2 Answers

You are passing a string to the bin() function:

>>> bin('10')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an index

Give it a integer instead:

>>> bin(10)
'0b1010'

by turning the raw_input() result to int():

n = int(raw_input())

Tip: you can easily reverse a string by giving it a negative slice stride:

>>> 'forward'[::-1]
'drawrof'

so you can simplify your function to:

def reversebinary():
    n = int(raw_input())
    bin_n = bin(n)[2:]
    revbin = bin_n[::-1]
    return int(revbin, 2)

or even:

def reversebinary():
    n = int(raw_input())
    return int(bin(n)[:1:-1], 2)
like image 58
Martijn Pieters Avatar answered Sep 22 '22 20:09

Martijn Pieters


You want to convert the input to an integer not a string - it's already a string. So this line:

n = str(raw_input())

should be something like this:

n = int(raw_input())
like image 45
James Holderness Avatar answered Sep 22 '22 20:09

James Holderness