【问题标题】:Rock, Paper, Scissors style game (Cowboy, Ninja, Bear)摇滚,纸,剪刀风格的游戏(牛仔,忍者,熊)
【发布时间】:2015-03-09 00:07:03
【问题描述】:

在我们找到here 的作业中,我们正在创建一个名为 Cowboy, Ninja, Bear 的游戏,它本质上是 Rock, Paper, Scissors。所以我有两个问题。

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

【问题讨论】:

  • 速记。这总是正确的:if userStr != "N" or userStr != "n" or userStr != "C" or userStr != "c" or userStr != "B" or userStr != "b" or userStr == "q" or userStr == "Q":
  • for 1) 不要生成数字,而是从列表中选择。即foo = ['c', 'b', 'n'] print(random.choice(foo))
  • for 2) 将用户输入转换为小写,即s.lower()

标签: python python-3.x random


【解决方案1】:

要随机分配三个字母中的一个,不必费心通过整数 - 只需随机分配三个字母之一!

computer = random.choice('cnb')

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

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

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

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

至于大写/小写规范化,只需:

userStr = userStr.lower()

它总是小写的。

【讨论】:

  • 非常感谢!至于 and vs or 我不知道我在打字时是怎么错过的。那是我应该抓住的。再次感谢您,尽管这个答案非常有帮助!
  • 使用 computer = random.choice('cnb') 行我收到此错误:Traceback(最近一次调用最后一次):文件“C:/Python34/cnb.py”,第 47 行,在 computer = random.randint("cnb") TypeError: randint() missing 1 required positional argument: 'b'
  • @Kryx:不是randint,而是choice
  • @Kryx,正如 Ethan 所说:你说的是 with random.choice,但错误消息明确告诉我们,这绝对是不是你正在使用的 - 相反,你是仍然调用randint,它代表“随机整数”,而您绝对想要一个整数?!
  • 不,我在自己的代码中将其更改为 random.choice('cnb') 并得到了确切的错误,然后我询问它是否有错字或我遗漏了一些小细节。但是我自己确实发现了错误,所以不用担心。感谢 Martelli 先生让我朝着正确的方向开始。
猜你喜欢
  • 2014-12-04
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
相关资源
最近更新 更多