简体   繁体   English

如何在 python 中允许输入为小写和大写?

[英]How to allow an input to be in lowercase and uppercase in python?

Here is my code.这是我的代码。 I'm trying to make a rock, paper, scissors game for a school project.我正在尝试为学校项目制作石头、剪子布游戏。

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")

The result you get is: Rock, Paper, Scissors?rock Rock, Paper, Scissors?Rock Rock, Paper, Scissors?Rock你得到的结果是:Rock, Paper, Scissors?rock Rock, Paper, Scissors?Rock Rock, Paper, Scissors?Rock

and it keeps going like this even though rock is part of choices.即使摇滚是选择的一部分,它也会继续这样下去。 This also happens if I input scissors in lowercase or paper.如果我以小写或纸张输入剪刀,也会发生这种情况。

You can convert your input as well as the concerned comparing value into the same case whether it may be in lowercase or uppercase by using .lower() or .upper() .您可以使用.lower().upper()将输入以及相关的比较值转换为相同的大小写,无论是小写还是大写。
Or while comparing like if player_choice.lower() == 'rock' ,或者在比较if player_choice.lower() == 'rock'时,
player_choice = player_choice.lower()

Here is another approach that you could consider:这是您可以考虑的另一种方法:

If your choices options need to be capitalized, if you capitalize the player_choice input, the user's input would then be able to match the choices options.如果您的choices选项需要大写,如果您将player_choice输入大写,则用户的输入将能够匹配choices选项。 This can be done with capitalize() .这可以通过capitalize()来完成。

Also, instead of having to worry about perfect uppercase or lowercase matching, you could try using regular expression matching using the re module.此外,不必担心完美的大写或小写匹配,您可以尝试使用re模块使用正则表达式匹配。 The matching could be done using re.match() with a special flag called re.IGNORECASE that lets you ignore the case of the words that are being checked.可以使用带有名为re.IGNORECASE的特殊标志的re.match()来完成匹配,该标志可让您忽略正在检查的单词的大小写。

Additionally, it might be a good idea to use elif: statements after the first if: statement for this type of check.此外,最好在第一个if:语句之后使用elif:语句进行此类检查。

I also added a space at the end of the initial input questions text for readability.为了便于阅读,我还在初始输入问题文本的末尾添加了一个空格。

Here is an example of what these changes could look like:以下是这些更改的示例:

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")

Just make your condition literals to lower case:只需将您的条件文字设为小写:

if player_choice == 'rock':

You can do:你可以做:

if player_choice.lower() == 'rock'如果 player_choice.lower() == 'rock'

This will take the user input and lowercase everything in it.这将接受用户输入并将其中的所有内容小写。

A better way would be to have both player and computer choose from the same set of values.更好的方法是让玩家和计算机从同一组值中进行选择。 The player enters values until they enter a valid choice.玩家输入值,直到他们输入一个有效的选择。 The computer uses random.choice() to select one of the choices from the valid list of choices.计算机使用random.choice()到 select 来自有效选项列表的选项之一。 Note that you can lower case the possible selections in the valid list:请注意,您可以将有效列表中的可能选择小写:

choices = ["rock", "paper", "scissors"]

Then it is easy to compare the computer and player selections by direct comparision.然后很容易通过直接比较来比较计算机和玩家的选择。 Here's code for the player and computer choices:这是播放器和计算机选择的代码:

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!! :>>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM