Home > Enterprise >  Not able to get variable to function in map input
Not able to get variable to function in map input

Time:04-30

How can i get variable to work in below code:

variable_data = 300
values = list(map(int, input('Enter two numbers {variable_data}: ').split())
a = values[0] if len(values) > 0 else 100
b = values[0] if len(values) > 0 else 200

result:
Enter two numbers {variable_data}:

CodePudding user response:

There is multiple solution.

First of all, you can use a f-string:

# You can use [*map] and not list(map) since Python 3.5, it's shorter
values = [*map(int, input(f'Enter two numbers {variable_data}: ').split())]

You can also manually concatenate an int to string:

values = [*map(int, input('Enter two numbers ' str(variable_data) ': ').split())]

But f-strings are the best option in this case.

So, this will look like that:

variable_data = 300
values = [*map(int, input('Enter two numbers {variable_data}: ').split())] # Enter two numbers 300: 
a = values[0] if len(values) > 0 else 100
b = values[0] if len(values) > 0 else 200

CodePudding user response:

use f-string

variable_data = 300
entry = input(f'Enter two numbers {variable_data}: ').split()
values = list(map(int, entry))
a = values[0] if len(values) > 0 else 100
b = values[0] if len(values) > 0 else 200
  • Related