0

I'm kind of new to python, and I wonder how you would convert the following code in C++ to python

int kids, cd;
while (cin >> kids >> cd){
    if (kids == 0 && cd == 0)
    break;
    cout << pow(cd, kids)<<endl;
}

So I think it's probably something like

    from math import pow
    kids = int(raw_input())
    cd = int(raw_input())
    while kids!=0 and cd!=0:
       print pow(cd, kids)
       kids = int(raw_input())
       cd = int(raw_input())

When I submit my python code to the online judge SPOJ, I got a runtime error. My C++ code isn't accepted because of the overflow when the numbers get too large... What's wrong?

Also when I tried to compile the python code using

http://repl.it/languages/Python

with input

2
5

and I get the output

25.0

But how do I make it to be 25 instead of 25.0?

Thanks!

3
  • Did you mean >> or should that be >=? Commented Jan 31, 2014 at 3:41
  • @Hugh: >> is probably intentional; cin >> x reads from the standard input into x. Commented Jan 31, 2014 at 3:43
  • Yes, >> is intentional; on each line, there will be two variables kids and cd to be read in Commented Jan 31, 2014 at 3:47

2 Answers 2

1

Your Python code is close to, but not quite like the original C++ code. In C++, you exit the loop on EOF or when both of the numbers are 0. In Python, you exit only when both of the numbers are zero, failing with a runtime error on EOF. Additionally, in C++, cin >> kids >> cd will read kids and cd from standard input separated by whitespace, whereas your Python code reads it separated by newlines. Finally, you're using integer operations in C++, but floating-point calculations in Python.

Let's start with the EOF problem. To catch an EOF, you do a read as usual and catch the EOFError exception, e.g.:

try:
    kids = int(raw_input())
    cd   = int(raw_input())
except EOFError:
    break  # out of the loop we're in (not shown)

The floating-point problem is also pretty easy to solve: pow is not necessary; rather, Python's got an operator for it: **. Thus, you've got

print cd ** kids

Lastly, there's that pesky delimiter problem. How you solve this depends on how your input looks. If you've got two inputs per line, e.g.:

1 2
3 4
5 6

Then you'd want to read the line once, split it up, and convert each piece to an integer:

try:
    line = raw_input()
except EOFError:
    break
kids, cd = line.split()
kids = int(kids)
cd   = int(cd)

There are other solutions for other formats, including a direct parallel of the C++ code; but that's more complex and may not be necessary.

Lastly, consider using a while True loop without repeating the kids = ...; cd = ... code at the start and breaking when you want to stop, as you did in the C++ code.

Sign up to request clarification or add additional context in comments.

Comments

1
while True:
    kids = int(raw_input())
    cd   = int(raw_input())
    if cd and kids:
        print(cd ** kids)
    else:
        break

Edit after looking at http://www.spoj.com/problems/UJ/ I would suggest

while True:
    n,d = [int(s) for s in raw_input().split()]
    if n and d:
        print(n ** d)
    else:
        break

3 Comments

Thank you, Hugh Bothwell. But your code still gives run time error for some reasons when I submitted it on the online judge SPOJ.
Here's the details: <br> number status signal time memory test 0 runtime error -1 0.03s 3740KB </br>
@user3213711: which SPOJ problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.