Back to General discussions forum
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?
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
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.
everything, replaced PASS by BREAK and everything worked! Thanks!
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...
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
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.
Oh thank you, Rodion, now I have the right direction for investigating
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 :)
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 !
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;
}
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.
sirpetethegreat, thank you very much for your help