我是Python的新手,正在从事这项作业。我不确定如何将random.choice分配给变量,因此可以继续执行if-then语句。
import random
def whoWin():
if userInput == choices
def main():
userInput = input("rock, paper, or scissor?")
print "You chose: " + userInput
choices = ["rock", "paper", "scissors"]
print "The computer chose: " + (random.choice(choices))
main()
答案 0 :(得分:1)
就像compInput = random.choice(choices)
一样简单,这意味着将从random.choice(choices)
获得的值分配给变量compInput
,您可以将其用于进一步的使用
此外,input
中使用了Python 3
,对于Python 2
,我们使用了raw_input
因此代码更改为
import random
userInput = raw_input("rock, paper, or scissor?")
print("You chose: " + userInput)
choices = ["rock", "paper", "scissors"]
#Assign random choice to variable
compInput = random.choice(choices)
print("The computer chose: " + compInput)
#If-else statement goes here
输出将为
rock, paper, or scissor?rock
You chose: rock
The computer chose: scissors