I’m trying to make simple program that asks the user “Who shot first?”
a) I need to know how I can make different inputs (such as: ‘Han’, ‘han’, ‘Han Solo’, and ‘han solo’) equal the variable “name”.
b) I need to know how to make the program print a new line after asking the user the question.
Here is what I have so far.
name = str(input('Who shot first?')) # Asks the user a question and stores the input as a variable.
if name == 'Han': # Replies accordingly to the users input.
print('Thats right!')
if name == 'Greedo':
print('Wrong! Greedo did not shoot first.')
else:
print('Incorect! Han shot first.')
I also just realized that I get this output when entering “Han” It prints both answers when it should only be printing one.
a) make the input string lowercase (make the strings in ifs to be lowercase too), then maybe split it by spaces (to get the words) and check if the word array contains relevant names (edge case if array contains both “han” and “greedo”);
b) add \n (line feed, aka new line) inside the input string.
About your edit, that’s coz you need 2nd if to be elif (aka else if)
# This program is to ask the user a question.
name = str(input('Who shot first?\n')) # Asks the user a question and stores the input as a variable.
if name in ('Han', 'han', 'han solo', 'Han Solo'): # Replies accordingly to the users input.
print('Thats right!')
elif name == 'Greedo':
print('Wrong! Greedo did not shoot first.')
else:
print('Incorect! Han shot first.')
you can use lower() to shorten your lists and account for capitalization errors:
name = input().lower()
if name in ['han', 'han solo', 'solo', 'hansolo', 'hs']:
print("That's right!")
elif name in ['greedo', 'gredo', 'g']:
print('Wrong!')
You can also use double quotes instead of single quotes in this case, since “That’s” is a contraction, and using single quotes would close the string before the “s”.