Python: Input two integers in a single line
Python Basic: Exercise-134 with Solution
Input Two Integers
Write a Python program to input two integers on a single line.
Sample Solution-1:
Python Code:
# Print a message to instruct the user to input the values of 'x' and 'y'.
print("Input the value of x & y")
# Use 'map' to apply the 'int' function to the input values and split them into variables 'x' and 'y'.
x, y = map(int, input().split())
# Print the values of 'x' and 'y' after they have been assigned.
print("The value of x & y are: ", x, y)
Sample Output:
Input the value of x & y 2 4 The value of x & y are: 2 4
Sample Solution-2:
Python Code:
# Prompt the user to input the values of 'a' and 'b' and split the input by spaces.
a, b = [int(a) for a in input("Input the value of a & b: ").split()]
# Print the values of 'a' and 'b' after they have been assigned.
print("The value of a & b are:", a, b)
Sample Output:
Input the value of a & b: 2 4 The value of a & b are: 2 4
Python Code Editor:
Previous: Write a Python program to calculate the time runs (difference between start and current time)of a program.
Next: Write a Python program to print a variable without spaces between values.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/python-basic-exercise-134.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics