在python中如何允许输入为小写和大写?
这是我的密码。我正在为一个学校项目做一个石头、布、剪刀的游戏在python中如何允许输入为小写和大写?,python,Python,这是我的密码。我正在为一个学校项目做一个石头、布、剪刀的游戏 import random choices = ["Rock", "Paper", "Scissors"] player_choice = input("Rock, Paper, Scissors?").lower() while player_choice not in choices: player_choice = input(&quo
import random
choices = ["Rock", "Paper", "Scissors"]
player_choice = input("Rock, Paper, Scissors?").lower()
while player_choice not in choices:
player_choice = input("Rock, Paper, Scissors?").lower()
computer_choice = random.randint(0,2)
if computer_choice == 0:
cpu_choice = choices[0]
elif computer_choice == 1:
cpu_choice = choices[1]
elif computer_choice == 2:
cpu_choice = choices[2]
print()
print("You play:", player_choice)
print("The computer plays:", cpu_choice)
print()
if player_choice == "Rock":
if cpu_choice == choices[0]:
print("Draw")
elif cpu_choice == choices[1]:
print("You Lose :((")
else:
print("You win!! :>>")
if player_choice == "Paper":
if cpu_choice == choices[0]:
print("You win!! :>>")
elif cpu_choice == choices[1]:
print("Draw")
else:
print("You Lose :((")
if player_choice == "Scissors":
if cpu_choice == choices[0]:
print("You Lose :((")
elif cpu_choice == choices[1]:
print("You Win!!! :>>")
else:
print("Draw")
结果是:
石头,布,剪刀?石头
石头,布,剪刀?石头
石头,布,剪刀?石头
尽管摇滚乐是选择的一部分,但它仍像这样继续着。如果我以小写或纸输入剪刀,也会发生这种情况。只需将条件文字设置为小写:
如果player\u choice=='rock':
只需将您的条件文字设置为小写:
如果玩家选择=='rock':
你可以做:
如果玩家选择。降低()
这将接受用户输入并将其中的所有内容小写。您可以执行以下操作:
如果玩家选择。降低()
这将使用用户输入,并将其中的所有内容小写。您可以使用
.lower()
或.upper()
将您的输入以及相关的比较值转换为相同的大小写,无论是小写还是大写 或者在比较时,如玩家选择.lower()='rock',
player\u choice=player\u choice.lower()
您可以使用.lower()
或.upper()
将您的输入以及相关的比较值转换为相同的大小写,无论是小写还是大写
或者在比较时,如玩家选择.lower()='rock',player\u choice=player\u choice.lower()
如果您的选择
选项需要大写,如果您将玩家选择
输入大写,则用户的输入将能够匹配选择
选项。这可以通过以下方式完成
此外,您可以尝试使用re
模块使用正则表达式匹配,而不必担心完美的大小写匹配。可以使用一个名为的特殊标志来完成匹配,该标志允许您忽略正在检查的单词的大小写
此外,对于这种类型的检查,最好在第一个if:
语句之后使用elif:
语句
为了便于阅读,我还在初始输入问题文本的末尾添加了一个空格
以下是这些更改的示例:
import random
import re
choices = ["Rock", "Paper", "Scissors"]
player_choice = input("Rock, Paper, Scissors? ").capitalize()
while player_choice not in choices:
player_choice = input("Rock, Paper, Scissors? ").lower()
computer_choice = random.randint(0,2)
if computer_choice == 0:
cpu_choice = choices[0]
elif computer_choice == 1:
cpu_choice = choices[1]
elif computer_choice == 2:
cpu_choice = choices[2]
print()
print("You play:", player_choice)
print("The computer plays:", cpu_choice)
print()
if re.match("rock", player_choice, flags=re.IGNORECASE):
if cpu_choice == choices[0]:
print("Draw")
elif cpu_choice == choices[1]:
print("You Lose :((")
else:
print("You win!! :>>")
elif re.match("Paper", player_choice, flags=re.IGNORECASE):
if cpu_choice == choices[0]:
print("You win!! :>>")
elif cpu_choice == choices[1]:
print("Draw")
else:
print("You Lose :((")
elif re.match("Scissors", player_choice, flags=re.IGNORECASE):
if cpu_choice == choices[0]:
print("You Lose :((")
elif cpu_choice == choices[1]:
print("You Win!!! :>>")
else:
print("Draw")
您可以考虑另一种方法:
如果您的选择
选项需要大写,如果您将玩家选择
输入大写,则用户的输入将能够匹配选择
选项。这可以通过以下方式完成
此外,您可以尝试使用re
模块使用正则表达式匹配,而不必担心完美的大小写匹配。可以使用一个名为的特殊标志来完成匹配,该标志允许您忽略正在检查的单词的大小写
此外,对于这种类型的检查,最好在第一个if:
语句之后使用elif:
语句
为了便于阅读,我还在初始输入问题文本的末尾添加了一个空格
以下是这些更改的示例:
import random
import re
choices = ["Rock", "Paper", "Scissors"]
player_choice = input("Rock, Paper, Scissors? ").capitalize()
while player_choice not in choices:
player_choice = input("Rock, Paper, Scissors? ").lower()
computer_choice = random.randint(0,2)
if computer_choice == 0:
cpu_choice = choices[0]
elif computer_choice == 1:
cpu_choice = choices[1]
elif computer_choice == 2:
cpu_choice = choices[2]
print()
print("You play:", player_choice)
print("The computer plays:", cpu_choice)
print()
if re.match("rock", player_choice, flags=re.IGNORECASE):
if cpu_choice == choices[0]:
print("Draw")
elif cpu_choice == choices[1]:
print("You Lose :((")
else:
print("You win!! :>>")
elif re.match("Paper", player_choice, flags=re.IGNORECASE):
if cpu_choice == choices[0]:
print("You win!! :>>")
elif cpu_choice == choices[1]:
print("Draw")
else:
print("You Lose :((")
elif re.match("Scissors", player_choice, flags=re.IGNORECASE):
if cpu_choice == choices[0]:
print("You Lose :((")
elif cpu_choice == choices[1]:
print("You Win!!! :>>")
else:
print("Draw")
更好的方法是让玩家和计算机从同一组值中进行选择。玩家输入值,直到输入有效选项。计算机使用random.choice()
从有效选项列表中选择一个选项。请注意,您可以将有效列表中可能的选项小写:
choices = ["rock", "paper", "scissors"]
然后通过直接比较很容易比较电脑和播放器的选择。以下是播放器和计算机选项的代码:
while True:
player_choice = input("Rock, Paper, Scissors? ").lower()
if player_choice in choices:
break
print("Invalid choice, try again.")
cpu_choice = random.choice(choices)
print()
print("You play:", player_choice)
print("The computer plays:", cpu_choice)
print()
if player_choice == cpu_choice:
print("Draw")
elif player_choice == 'rock':
if cpu_choice == 'paper':
print("You Lose :((")
else:
print("You win!! :>>")
elif player_choice == 'paper':
if cpu_choice == 'scissors':
print("You Lose :((")
else:
print("You win!! :>>")
else: # player chose scissors
if cpu_choice == 'rock':
print("You Lose :((")
else:
print("You win!! :>>")
更好的方法是让玩家和计算机从同一组值中进行选择。玩家输入值,直到输入有效选项。计算机使用random.choice()
从有效选项列表中选择一个选项。请注意,您可以将有效列表中可能的选项小写:
choices = ["rock", "paper", "scissors"]
然后通过直接比较很容易比较电脑和播放器的选择。以下是播放器和计算机选项的代码:
while True:
player_choice = input("Rock, Paper, Scissors? ").lower()
if player_choice in choices:
break
print("Invalid choice, try again.")
cpu_choice = random.choice(choices)
print()
print("You play:", player_choice)
print("The computer plays:", cpu_choice)
print()
if player_choice == cpu_choice:
print("Draw")
elif player_choice == 'rock':
if cpu_choice == 'paper':
print("You Lose :((")
else:
print("You win!! :>>")
elif player_choice == 'paper':
if cpu_choice == 'scissors':
print("You Lose :((")
else:
print("You win!! :>>")
else: # player chose scissors
if cpu_choice == 'rock':
print("You Lose :((")
else:
print("You win!! :>>")
你不需要你的if条件字是小写的吗?既然您在用户输入时调用lower,那么您不需要将if条件文字设置为小写吗?因为您在用户输入上调用了lower?