Jump to content

Not able to solve simple python programming question


gugtenterf

Recommended Posts

Question-

TaskThe provided code stub reads two integers from STDIN,  and . Add code to print three lines where:
The first line contains the sum of the two numbers.
The second line contains the difference of the two numbers (first - second).
The third line contains the product of the two numbers.
Example
Print the following:
8 -2 15

 

my answer

if __name__=='__main__':
    a=input(int())
    b=input(int())
    
if 1<=a<=10**10 and 1<=b<=10**10:
    summation = a+b
    diff = a-b
    multiply = a*b

print(summation)
print(diff)
print(multiply)

 

error

Traceback (most recent call last):
File "Solution.py", line 6, in <module>
if 1<=a<=10**10 and 1<=b<=10**10:
TypeError: '<=' not supported between instances of 'int' and 'str'
Link to comment
Share on other sites

3 minutes ago, gugtenterf said:

Question-

TaskThe provided code stub reads two integers from STDIN,  and . Add code to print three lines where:
The first line contains the sum of the two numbers.
The second line contains the difference of the two numbers (first - second).
The third line contains the product of the two numbers.
Example
Print the following:
8 -2 15

 

my answer

if __name__=='__main__':
    a=input(int())
    b=input(int())
    
if 1<=a<=10**10 and 1<=b<=10**10:
    summation = a+b
    diff = a-b
    multiply = a*b

print(summation)
print(diff)
print(multiply)

 

error

Traceback (most recent call last):
File "Solution.py", line 6, in <module>
if 1<=a<=10**10 and 1<=b<=10**10:
TypeError: '<=' not supported between instances of 'int' and 'str'

https://pythondev.online/

thanks in advance for any help

Edited by gugtenterf
Link to comment
Share on other sites

  • 1 month later...

a = [4,5,6]
b = [1,2,3]

answer_list = [(x[0]+x[1],x[0]-x[1], x[0]*x[1]) for x in zip(a,b)]

for answer in answer_list:
    print("add",answer[0])
    print("subtract",answer[1])
    print("product", answer[2])
    print()

************************

add 5
subtract 3
product 4

add 7
subtract 3
product 10

add 9
subtract 3
product 18
Link to comment
Share on other sites

with numpy:

**********************************

import numpy as np

arr1 = np.array(range(1,4))      # [1,2,3]
arr2 = np.array(range(4,7))     # [4,5,6]

add_arr = arr2 + arr1
print("sums",add_arr)
sub_arr = arr2 - arr1
print("differences",sub_arr)
multi_arr = arr2 * arr1
print("products",multi_arr)

***************************

sums [5 7 9]
differences [3 3 3]
products [ 4 10 18]
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...