What is the Python equivalent to this C++ code that reads from stdin? -
i have joined dark side , have decided learn python. using python 3.
here straight forward way using c++ read 2 integers @ time until both of them 0:
int x, y; while (cin >> x >> y && (x != 0 || y != 0)) { //x or y can 0 not both } //now x = 0 , y = 0 or can't find 2 int's
it's easy, simple , works 99.999% of time. have following in python doesn't seem pythonic me. also, doomed fail on inputs (i.e. if int's on 2 different lines)
while true: line = sys.stdin.readline() values = [int(i) in line.split()] if (values[0] == 0 , values[1] == 0): break x = values[0] y = values[1] print(x + y) print("both 0 or couldn't find 2 int's")
can please tell me cleanest, pythonic way read 2 int's @ time until both 0 using python 3?
try out. simple tests seem work. throw error if type 1 number, though.
while true: x,y = map(int, input().split()) if x == 0 , y == 0: break print(x + y) print("both 0 or couldn't find 2 int's")
this version correctly handles "couldn't find 2 int's" case.
while true: line = input() data = line.split() if len(data) < 2: break x,y = map(int, data) if x == 0 , y == 0: break print(x + y) print("both 0 or couldn't find 2 int's")
Comments
Post a Comment