在菜单中要求2个输入

时间:2014-03-27 00:33:00

标签: python

我被困在学校的一些工作上。我需要创建一个2人的石头剪刀游戏(playerOne,cpu)cpu需要使用随机数生成器。我的问题是我不知道如何让程序从菜单中请求第二个输入。

        menuChoice= eval(input("Enter your choice here: "))
        if menuChoice == 1:
            subTotal += PIZZA_PRICE
            #display subTotal        

这就是我引导的例子,但它仅用于一次输入。

while subMenuOne != 4:
  #Prompt for playerOne subMenuOne choice

  #prompt for cpu subMenuchoice

     if subMenuOne == 1:
        playerOne = Rock
        cpu = Rock
     if (playerOne == cpu):
        #draw
        print("It's a draw. You get to fight another day..")

    elif (playerOne == "Rock" and cpu == "Scissors" or
          playerOne == "paper" and cpu == "rock" or
          playerOne == "scissors" and cpu == "paper"):
             #win
             print("You Won!")
    else:
             #lose
             print("You're Dead. Better Luck Next Time")

如何让程序询问2个值,以便能够找出结果。同样对于CPU选择,我被告知使用它:

#random number generator
from random import randrange
def main():
    #randomNumber as integer
    #generate a random number from 1 to 3
    randomNumber = randrange(1,4)
    print (randomNumber)

我是否在正确的轨道上至少与代码中的其他陈述一致?谢谢你的帮助。

编辑:这是我到目前为止所有人都想看看的内容。 http://pastebin.com/fhdgVYG9

1 个答案:

答案 0 :(得分:1)

您可以使用random模块,特别是计算机部件的choice方法来完成此操作。对于播放器部分,您可以继续询问输入,直到输入有效。这里有一些代码可以帮助您入门:

from random import choice

player_input = ''
options = ['rock','paper','scissors']

while player_input.lower() not in options: #keep repeating while player response is not a valid option
    player_input = str(raw_input('Please enter valid input: ')).lower()

computer_guess = choice(options)

... #Here you run the comparisons similar to as you have shown in the code above.
#Except, make sure all the string comparisons you make are in lowercase