剪刀石头布游戏 [英] Rock-Paper-Scissors Game
问题描述
我目前非常喜欢这个石头,纸,剪刀程序,将不胜感激.我浏览过其他有关岩石,纸张,剪刀程序的帖子,但我仍然很困惑.
I'm currently pretty stuck on this rock, paper, scissors program and would greatly appreciate some help. I have looked through other posts concerning rock, paper, scissors programs but I'm still stuck.
我当前遇到的错误是当我要求用户选择"Rock","Paper"或"Scissors"时,它将继续询问几次,然后出现错误.另外,在我看来,我看过的大部分文章都涉及我在课堂上没有使用过的概念,因此我对它们不满意.
The error I'm getting currently is When I ask the user to choose 'Rock', 'Paper' or 'Scissors' it will keep asking it a couple more times and then I get an error. Also, it seems to me that a good portion of the posts I look at involve concepts that I haven't used in class, so I'm not comfortable with them.
choices = [ 'Rock', 'Paper', 'Scissors' ]
# 1. Greetings & Rules
def showRules():
print("\n*** Rock-Paper-Scissors ***\n")
print("\nEach player chooses either Rock, Paper, or Scissors."
"\nThe winner is determined by the following rules:"
"\n Scissors cuts Paper -> Scissors wins"
"\n Paper covers Rock -> Paper wins"
"\n Rock smashes Scissors -> Rock wins\n")
# 2. Determine User Choice
def getUserChoice():
usrchoice = input("\nChoose from Rock, Paper or Scissors: ").lower()
if (usrchoice not in choices):
usrchoice = input("\nChoose again from Rock, Paper or Scissors: ").lower()
print('User chose:', usrchoice)
return usrchoice
# 3. Determine Computer choice
def getComputerChoice():
from random import randint
randnum = randint(1, 3)
cptrchoice = choices(randnum)
print('Computer chose:', cptrchoice)
return randnum
# 4. Determine Winner
def declareWinner(user, computer):
if usrchoice == cptrchoice:
print('TIE!!')
elif (usrchoice == 'Scissors' and cptrchoice == 'Rock'
or usrchoice == 'Rock' and cptrchoice == 'Paper'
or usrchoice == 'Paper' and cptrchoice == 'Scissors'):
print('You lose!! :(')
else:
print('You Win!! :)')
#5. Run program
def playGame():
showRules() # Display the title and game rules
user = getUserChoice() # Get user selection (Rock, Paper, or Scissors)
computer = getComputerChoice() # Make and display computer's selection
declareWinner(user, computer) # decide and display winner
推荐答案
您的代码有一些问题:
首先是将user input
转换为小写字母,但列表项不是.因此检查将失败.
First is you are converting user input
to lowercase but your list items are not. So the check will fail.
choices = [ 'rock', 'paper', 'scissors' ]
第二件事是您正在调用choice(randnum),这将引发错误,因为您必须使用[]
从列表中检索元素.
Second thing is you are calling choice(randnum) which will throw an error as you have to use []
to retrieve element from list.
cptrchoice = choices[randnum]
如果输入无效的字符串,会发生第三种情况.您只需使用if
进行检查,但需要while loop
Third is what happens if you enter invalid string. You only check with if
but you need while loop
while (usrchoice not in choices):
usrchoice = getUserChoice() #input("\nChoose again from Rock, Paper or Scissors: ").lower()
第四位在declareWinner
中,您的params
是user
和computer
,但是随后您在if
条件下使用usrchoice
和cptrchoice
Fourth is in declareWinner
, your params
are user
and computer
but then you are using usrchoice
and cptrchoice
in if
conditions
def declareWinner(usrchoice, cptrchoice):
if usrchoice == cptrchoice:
尝试一下并试一试
这篇关于剪刀石头布游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!