Python输入错误如何处理
How to take Python Input with mistakes
我正在制作一个脚本,你可以根据其中的一句话来猜测一部电影。我想知道我是如何做到的,以便我可以使用 "Terminator" 和 "The Terminator" 并允许拼写错误仍然正确。
我试图查找它,但几乎一无所获。
#Guess that movie, gives a quote and you have to guess it for points, add a high score system.
from random import randint
points = 0
quote1 = randint(0,3)
quote2 = randint(0,3)
quote3 = randint(0,3)
quote4 = randint(0,3)
movieQuoteEasy = ["You're going to need a bigger boat.", "I'll be back.", "Here's Johnny!", "Say hello to my little friend!"]
movieQuoteMedi = ["Luca Brazi Sleeps with the fishes.", "Whatever doesn't kill you simply makes you... stranger.", "You talking to me?", "I love the smell of Napalm in the morning."]
movieQuoteHard = ["Rosebud...", "How am I funny to you? what makes me so funny.", "They call it a Royal with Cheese.", "Go ahead, make my day."]
movieQuoteExtr = ["I tried... at least I did that.", "Gentlemen, you can't fight here this is the way room!", "I'm having an old friend for Dinner.", "The greatest trick the devil pulled was convincing the world he didn't exist."]
movieAnswerEasy = ["Jaws", "The Terminator", "The Shining", "Scarface"]
movieAnswerMedi = ["The Godfather", "The Dark Knight", "Taxi Driver", "Apocalypse Now"]
movieAnswerHard = ["Casablanca", "Goodfellas", "Pulp Fiction", "Dirty Hary"]
movieAnswerExtr = ["One Flew Over the Cuckos Nest", 'Dr. Strangelove', "Silence of the Lambs", "The Usual Suspects"]
print("Welcome to Guess That Movie!")
input()
#Easy Question
print("Easy: " + movieQuoteEasy[quote1])
guess1 = input()
#Takes the value for use input and checks it against the correct answer.
if guess1 == movieAnswerEasy[quote1]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerEasy[quote1])
#Medium Question
print("Medium: " + movieQuoteMedi[quote2])
guess2 = input()
#Takes the value for use input and checks it against the correct answer.
if guess2 == movieAnswerMedi[quote2]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerMedi[quote1])
#Hard Question
print("Hard: " + movieQuoteHard[quote3])
guess3 = input()
#Takes the value for use input and checks it against the correct answer.
if guess3 == movieAnswerHard[quote3]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerHard[quote3])
#Extream Question
print("Insane: " + movieQuoteExtr[quote4])
guess4 = input()
# Takes the value for use input and checks it against the correct answer.
if guess4 == movieAnswerExtr[quote4]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerExtr[quote4])
print("\nGreat job, you have " + str(points) + " points.")
input()
exit()
我希望能够接受 'The Terminator' 而只是 'terminator'
我知道如何解决你的问题。您可以做的第一件事是将 .lower()
添加到输入的末尾。这会将用户输入中的所有字母都变成小写。像这样:guess1 = input().lower
。现在,用户输入大写还是小写都没有关系。还;你应该这样做:
guess1 = input(">>> ").lower() #Allows the user to input lower case letters.
if "terminator" in guess1: #This is probably what you are looking for.
#If the program detects the keyword 'terminator'
#in your guess, then the if statement will execute.
#Whatever happens when the user is correct.
else:
#Whatever happens when the user is incorrect.
希望对你有所帮助!
我正在制作一个脚本,你可以根据其中的一句话来猜测一部电影。我想知道我是如何做到的,以便我可以使用 "Terminator" 和 "The Terminator" 并允许拼写错误仍然正确。
我试图查找它,但几乎一无所获。
#Guess that movie, gives a quote and you have to guess it for points, add a high score system.
from random import randint
points = 0
quote1 = randint(0,3)
quote2 = randint(0,3)
quote3 = randint(0,3)
quote4 = randint(0,3)
movieQuoteEasy = ["You're going to need a bigger boat.", "I'll be back.", "Here's Johnny!", "Say hello to my little friend!"]
movieQuoteMedi = ["Luca Brazi Sleeps with the fishes.", "Whatever doesn't kill you simply makes you... stranger.", "You talking to me?", "I love the smell of Napalm in the morning."]
movieQuoteHard = ["Rosebud...", "How am I funny to you? what makes me so funny.", "They call it a Royal with Cheese.", "Go ahead, make my day."]
movieQuoteExtr = ["I tried... at least I did that.", "Gentlemen, you can't fight here this is the way room!", "I'm having an old friend for Dinner.", "The greatest trick the devil pulled was convincing the world he didn't exist."]
movieAnswerEasy = ["Jaws", "The Terminator", "The Shining", "Scarface"]
movieAnswerMedi = ["The Godfather", "The Dark Knight", "Taxi Driver", "Apocalypse Now"]
movieAnswerHard = ["Casablanca", "Goodfellas", "Pulp Fiction", "Dirty Hary"]
movieAnswerExtr = ["One Flew Over the Cuckos Nest", 'Dr. Strangelove', "Silence of the Lambs", "The Usual Suspects"]
print("Welcome to Guess That Movie!")
input()
#Easy Question
print("Easy: " + movieQuoteEasy[quote1])
guess1 = input()
#Takes the value for use input and checks it against the correct answer.
if guess1 == movieAnswerEasy[quote1]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerEasy[quote1])
#Medium Question
print("Medium: " + movieQuoteMedi[quote2])
guess2 = input()
#Takes the value for use input and checks it against the correct answer.
if guess2 == movieAnswerMedi[quote2]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerMedi[quote1])
#Hard Question
print("Hard: " + movieQuoteHard[quote3])
guess3 = input()
#Takes the value for use input and checks it against the correct answer.
if guess3 == movieAnswerHard[quote3]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerHard[quote3])
#Extream Question
print("Insane: " + movieQuoteExtr[quote4])
guess4 = input()
# Takes the value for use input and checks it against the correct answer.
if guess4 == movieAnswerExtr[quote4]:
print("Correct!")
points += 5
print("You have " + str(points) + " points!")
else:
print("Wrong, the correct answer was " + movieAnswerExtr[quote4])
print("\nGreat job, you have " + str(points) + " points.")
input()
exit()
我希望能够接受 'The Terminator' 而只是 'terminator'
我知道如何解决你的问题。您可以做的第一件事是将 .lower()
添加到输入的末尾。这会将用户输入中的所有字母都变成小写。像这样:guess1 = input().lower
。现在,用户输入大写还是小写都没有关系。还;你应该这样做:
guess1 = input(">>> ").lower() #Allows the user to input lower case letters.
if "terminator" in guess1: #This is probably what you are looking for.
#If the program detects the keyword 'terminator'
#in your guess, then the if statement will execute.
#Whatever happens when the user is correct.
else:
#Whatever happens when the user is incorrect.
希望对你有所帮助!