Problem 14 Modular Calculator

Back to General discussions forum

Andrey_Alexandr     2020-01-15 11:37:42

Hi

My solution:

n = int(input())
while True:
  inp=input().split()
  if inp[0]=="+":
    n+=int(inp[1])
  elif inp[0]=="*":
    n*=int(inp[1])
  elif inp[0]=="%":
    print(n % int(inp[1]))
    pass

In PyCharm all work.

But on the task page, in the "your answer" section, he writes: Your answer: paste here an answer from your program Traceback (most recent call last): File "solution.py", line 3, in <module> inp=input().split()EOFError: EOF when reading a line

I do not understand what the problem is. Please, help?

Rodion (admin)     2020-01-15 13:44:16
User avatar
while True

You are trying to read endlessly - relying on the fact that program should stop when input ends.

This is not the great idea. As you see, it may work in some system and fail in other.

Just break out of the loop instead of pass, I think

Andrey_Alexandr     2020-01-15 14:07:01

If I understood correctly, I replaced WHILE with a FOR cycle with repetitions with a margin, the error remained:

n = int(input())
for i in range(100):
    inp=(input().split())
    if inp[0]=="+":
        n+=int(inp[1])
    elif inp[0]=="*":
        n*=int(inp[1])
    elif inp[0]=="%":
        print(n % int(inp[1]))
        pass

In PyCharm all work.

Andrey_Alexandr     2020-01-15 14:11:37

everything, replaced PASS by BREAK and everything worked! Thanks!

Rodion (admin)     2020-01-15 15:00:54
User avatar

If I understood correctly, I replaced WHILE with a FOR cycle

no, not correctly :)

the matter is you shouldn't do more iterations than input have data. You should not call input() after the line with % at beginning (which is last).

for ... 100 won't work because there may be less than 100 lines. so you should exit from loop when you find end of input. it is either break or some flag like this:

has_more_data = True
while has_more_data:
    inp=input()...
    ...
    if inp[0]=='%':
        print...
        has_more_data = False

In PyCharm all work.

not all :) in PyCharm your program just won't exit and waits for more data. I recommend you to learn running python from command line and feeding input from file to it, as real applications usually are not working in PyCharm :)

Though yet alternatively you can just catch the error on the end of input with try ... except. So you see, there are many ways as usually...

shatseli     2020-01-16 18:34:29

Rodion, Sorry but it is obviously that for this task server is sending the wrong status. Despite the type DOUBLE defined for all intermediate Java varaibles and result being checked manually the site is still erroneously sending the "Wrong" status

Rodion (admin)     2020-01-16 18:51:48
User avatar

Hey, please stop spamming. See my answer here:

https://www.codeabbey.com/index/forum_topic/351fcadb37047faffbe71d9d397f7de6#lastPost

Despite the type DOUBLE

Double won't help you unless you solve problem correctly. It doesn't hold infinite numbers.

shatseli     2020-01-17 16:53:17

Oh thank you, Rodion, now I have the right direction for investigating

Rodion (admin)     2020-01-17 16:59:22
User avatar

Forgot to mention - in Java you can use infinite-precision numbers with type BigInt or BigDecimal.

though for this problem it is not needed (as I tried to explain in the second post).

in languages like Python however integers are infinite by default... that's feels like cheating :)

shatseli     2020-01-17 17:21:36

Thank you very much Rodion for you great hint ! I planned to practique in in Python after Java and your remark gives a lot of understanding the basics. Have a nice weekend !

Leva_Malc     2020-05-20 13:53:27

please help what am i doing wrong?

int main() { unsigned long long int x,n; char operation=' ';

std::cin >> x;

while (operation != '%') {
    std::cin >> operation >> n;
    switch (operation)
    {
    case '+':      
        x += n;
        break;
    case '*':
        x *= n;
        break;
    case '%':
        x = x%n;
        break; 
    }
}

std::cout << x;
return 0;

}

sirpetethegreat     2020-05-21 01:49:53

Leva_Malc,

In this case long integers won't hold big enough values to make the "straightforward" approach work. You need to implement some interesting part of mathematics to do this correctly. There's a link in the opening paragraph to some additional information you're going to need.

Leva_Malc     2020-05-22 09:14:44

sirpetethegreat, thank you very much for your help

Please login and solve 5 problems to be able to post at forum