石头,布,剪刀式游戏(牛仔,忍者,熊)

2024-06-28 02:49:45 发布

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

在我们的作业中发现here我们正在创造一个叫做牛仔,忍者,熊的游戏,基本上是石头,布,剪刀。所以我有两个问题。在

1.)如何将c、n或b分配给程序1、2或3生成的随机数?在

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

Tags: orandofyouforinputifcounter
2条回答

要随机分配三个字母中的一个,不要费心去处理一个整数,只需随机分配三个字母中的一个!在

computer = random.choice('cnb')

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

^{pr2}$

userStr总是不同于'N'或者不同于{}(等等),毕竟如果它等于一,它就不能等于另一个,不是吗?!在

所以这里你必须使用and不是or。。。!-)在

至于规范化大写/小写,只需执行以下操作:

userStr = userStr.lower()

它总是小写的。在

相关问题 更多 >