石头剪刀布风格游戏(牛仔、忍者、熊)

Rock, Paper, Scissors style game (Cowboy, Ninja, Bear)

在我们的作业中发现 here 我们正在创建一个名为 Cowboy, Ninja, Bear 的游戏,本质上是石头剪刀布。所以我有两个问题。

1.) 1、2、3的程序生成的随机数如何赋值给c、n、b?

2.) 在比较程序的选择和用户的选择时,有没有一种快速简便的方法来确保N 等于n 以缩短代码?所以它只需要比较c、b和n而不是把C、B和N也扔进循环?

"""
Author: 
Program: cnb.py
Description: A game of Cowboy, Ninja, Bear. Similar to Rock, Paper, Scissors.
The user picks either Ninja, Cowboy, or Bear. The program compares this against
its own randomly generated choice and replys with the rounds outcome. The program
also keeps track of the number of wins, losses, ties, and overall rounds.
"""

import random
random.seed()

counter = 1
winCounter = 0
loseCounter = 0
tieCounter = 0

#Input Rules

print ("Enter:")
print ("  'C' or 'c' for Cowboy")
print ("  'N' or 'n' for Ninja")
print ("  'B' or 'b' for Bear")
print()


#Prompt user for input

print ("Round", counter, "Fight!")
userStr = input("Please enter a weapon: ")



#If user input is not compliant with rules instruct
#user to retry.

if userStr == "q" or userStr == "Q":
    print("Game Over!")
if userStr != "N" or userStr != "n" or userStr != "C" or userStr != "c" or userStr != "B" or userStr != "b" or userStr == "q" or userStr == "Q":
    print ()
    print ("That's not a valid choice!")
    userStr = input("Please enter a weapon: ")

#Computer picks Weapon

computer = random.randint(1,3)



#Compare Results and print results

#Win
if userStr == c and computer == c
    winCounter = winCounter + 1
    print ("You win")
if userStr == n and computer == n
    winCounter = winCounter + 1
    print ("You win")
if userStr == b and computer == b
    winCounter = winCounter + 1
    print ("You win")

#Loss
if userStr == c and computer == c
    lossCounter = lossCounter + 1
    print ("You lose")
if userStr == n and computer == n
    lossCounter = lossCounter + 1
    print ("You lose")
if userStr == b and computer == b
    lossCounter = lossCounter + 1
    print ("You lose")

#Tie
if userStr == c and computer == c
    tieCounter = tieCounter + 1
    print ("You tied")
if userStr == n and computer == n
    tieCounter = tieCounter + 1
    print ("You tied")
if userStr == b and computer == b
    tieCounter = tieCounter + 1
    print ("You tied")



#Loop to new round

counter = counter + 1
print()
print ("Round", counter)
userStr = input("Please enter a weapon: ")

要随机分配三个字母之一,不要费心去检查一个整数——只需随机分配三个字母之一!

computer = random.choice('cnb')

但是,您的代码中还有其他错误,例如

if userStr != "N" or userStr != "n" or (etc etc)

userStr总是 不同于 'N' 或不同于 'n'(等等)——毕竟,如果它等于 1 ,它不能也等于另一个,不是吗?!

所以这里一定要用and不是 or...!-)

至于标准化 upper/lower 大小写,只需执行:

userStr = userStr.lower()

并且它总是小写。