0

I want to save an user input directly into two variables like:

n,k = input("Geben Sie eine Zahl ein: " )

print(n)
print(k)

n and k should print the same value.

2
  • 3
    Why do you tag this with Java? Commented Jul 7, 2017 at 16:35
  • 4
    That would be n = k = ... Commented Jul 7, 2017 at 16:35

3 Answers 3

3

You can do this with a double assignment:

n = k = input("Geben Sie eine Zahl ein: " )

Your n,k = ... however is iterable unpacking: only if the user would enter two characters, this would not error: in that case the first character would be assigned to n, and the second character to k. So if you would have written fo, then n would have value 'f' and k would have value 'o'. But for all other cases, this would fail.

Be careful however with mutable objects: if you write:

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

Then both variables refer to the same list object. Only one list is constructed. And if you modify the list through a, then you will see the difference through b as well.

Sign up to request clarification or add additional context in comments.

Comments

2

Try:

n = k = input("Geben Sie eine Zahl ein: " )

Comments

1

for same value in two variable:

n = k = input("enter two values:")

for two value in two variable:

n, k = input("enter two values:").split(' ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.