0

I have a simple if-else that takes a command-line argument and returns a value from a dictionary. When I enter 'Darth Vader', it returns 'Luke, I am your None' instead of 'No, I am your father'.

It works as expected with all other keys in the dictionary. Here is the code:

import sys

relations = {'Darth Vader': 'father', 'Leia': 'sister', 'Han': 'brother in law',
'R2D2': 'droid', 'Rey': 'Padawan', 'Tatooine': 'homeworld'}

key = sys.argv[1]

if key == 'Darth Vader':
  print("No, I am  your father")
else:
  print('Luke, I am your', relations.get(key))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
A1live
  • 1

2 Answers2

1
key = sys.argv[1]

This gives you the second argument from the cmd (command line), but this leads to an error, since with the space between "Darth" and "Vader" makes "Darth" to be the only second argument.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
0

As said Patrick Haugh, you should rename Darth Vader Darth_Vader, or use quotes ("Darth Vader") in your arguments instead of Darth Vader.