Python将字符串与lis中的随机项进行比较

2024-04-25 13:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

看了很多现有的问题,没有看到完全相同的情况。我正在学习Python的基础知识,并尝试创建无处不在的石头、布、剪刀游戏。然而,我有一个变量等于用户的猜测,我无法成功地与代表计算机猜测的随机选择的项目进行比较。所有的评价都是“你赢了!:)“,即使不应该,这表明这两个变量永远不相等。我尝试过使用str(computer\u choice)而没有任何运气。你知道吗

以下是完整的代码:

import time
import random

rps = ['Rock', 'Paper', 'Scissors']

computer_choice = random.sample(rps, 1)

print("Let's play some Rock, Paper, Scissors")
print("Hit enter when ready:")
input("")

print("Rock...")
time.sleep(1)
print("Paper...")
time.sleep(1)
print("Scissors...")
time.sleep(.75)
print("SHOOT!")

print("Which is it?")
user_choice = input(">>>")



while True:
    valid_check = user_choice in rps
    if valid_check == True:
        break
    else:
        print('Bad input, try again:')
        user_choice = input(">>>")

print("The computer picked: %s" % computer_choice)

if user_choice == computer_choice:
    print("It's a draw!")

elif user_choice == 'Rock':
    if computer_choice == 'Paper':
        print("You lose :(")

    else:
        print("You win! :)")

elif user_choice == 'Paper':
    if computer_choice == 'Scissors':
        print("You lose :(")

    else:
        print("You win! :)")

elif user_choice == 'Scissors':
    if computer_choice == 'Rock':
        print("You lose :(")

    else:
        print("You win! :)")

Tags: youinputiftimesleepelsecomputerpaper