Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

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

and it keeps going like this even though rock is part of choices. This also happens if I input scissors in lowercase or paper.

question from:https://stackoverflow.com/questions/66056827/how-to-allow-an-input-to-be-in-lowercase-and-uppercase-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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().
Or while comparing like if player_choice.lower() == 'rock',
player_choice = player_choice.lower()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...