Home > database >  Input function for multiple values in python
Input function for multiple values in python

Time:06-25

a,b=int(input("Enter two numbers").split())

print(a b)

I have written this code to add two numbers while taking input using input() function. It is giving an error. I know we can convert to int using map function but how about this method? What is the error here? [The pic depicts th error which it gives]

CodePudding user response:

split returns a list of strings, so you need to call int on each of the items in that list. It doesn't make sense to call int on a list. For example

>>> a, b = [int(x) for x in "1 2".split()]
>>> a
1
>>> b
2
>>> 

CodePudding user response:

You could try this (Using map function).

a,b=map(int, input("Enter two numbers").split())

print(a b)

CodePudding user response:

x, y = input("Enter two values: ").split()

print(int(x) int(y))
  • Related