初学者摇滚,纸艺,剪刀游戏Python语法

时间:2018-06-22 05:39:16

标签: python

我创建了一个Rock Paper Scissors游戏。它会询问您是否要玩,接受您的输入,将其与计算机的随机输入进行比较,为您提供结果,然后询问您是否要再次玩。

我不知道为什么它不起作用。请让我知道如何解决它。谢谢

import random
import sys

#play
def play():
  print('want to play?')
if input()=='yes':
  print("Pick R/P/S")
  user = input()



# RPS
def RPS():
  x = random.choice( ['Rock', 'Paper', 'scissors'] )
print(x)
if x == 'Rock' and user == 'R':
  print('Tie')
elif x == 'Rock' and user == 'P':
  print('You beat Comp')
elif x == 'Rock' and user == 'S':
  print('Beat you')
#--------
if x == 'Paper' and user == 'R':
  print('Lost to comp')
elif x == 'Paper' and user == 'P':
  print('tie')
elif x == 'Paper' and user == 'S':
  print('you beat comp')
#---------
if x == 'scissors' and user == 'R':
  print('Beat comp')
elif x == 'scissors' and user == 'P':
  print('lost comp')
elif x == 'scissors' and user == 'S':
  print('tie')

#Play again
def playagain():
  print('Want to play again')
  play=input()
if play=='yes':
    RPS()
else:
    print("Thanks for playing")



play()
RPS()
playagain()

2 个答案:

答案 0 :(得分:0)

您有一些缩进问题。另外,您需要在全局范围内声明user变量。看一下这段代码:

import random
import sys
user = None

def play():
    print('want to play?')
    if input()=='yes':
      print("Pick R/P/S")
      global user
      user = input()


def RPS():
    x = random.choice( ['Rock', 'Paper', 'scissors'] )
    print(x)
    if x == 'Rock' and user == 'R':
      print('Tie')
    elif x == 'Rock' and user == 'P':
      print('You beat Comp')
    elif x == 'Rock' and user == 'S':
      print('Beat you')
    #--------
    if x == 'Paper' and user == 'R':
      print('Lost to comp')
    elif x == 'Paper' and user == 'P':
      print('tie')
    elif x == 'Paper' and user == 'S':
      print('you beat comp')
    #---------
    if x == 'scissors' and user == 'R':
      print('Beat comp')
    elif x == 'scissors' and user == 'P':
      print('lost comp')
    elif x == 'scissors' and user == 'S':
      print('tie')

def playagain():
    print('Want to play again')
    play=input()
    if play=='yes':
        RPS()
    else:
      print("Thanks for playing")

play()
RPS()
playagain()

答案 1 :(得分:0)

尝试一下,这应该可行。我刚刚编辑了您的代码,以使其更好一点:

import random
import sys
con_1 = True
ask_to_play = input('want to play?:')
if ask_to_play=='yes':
    while con_1:
        print("Pick R/P/S")
        user = input()
        x = random.choice( ['Rock', 'Paper', 'scissors'] )
        print(x)
        if x == 'Rock' and user == 'R':
            print('Tie')
        elif x == 'Rock' and user == 'P':
            print('You beat Comp')
        elif x == 'Rock' and user == 'S':
            print('Beat you')
    #--------
        if x == 'Paper' and user == 'R':
            print('Lost to comp')
        elif x == 'Paper' and user == 'P':
            print('tie')
        elif x == 'Paper' and user == 'S':
            print('you beat comp')
    #---------
        if x == 'scissors' and user == 'R':
            print('Beat comp')
        elif x == 'scissors' and user == 'P':
            print('lost comp')
        elif x == 'scissors' and user == 'S':
            print('tie')
        print('Want to play again(Y/n)?:')
        play=input()
        if play=='Y' or play == "y":
            con_1 = True
        else:
            print("Thanks for playing")
            con_1 = False
else:
    print("I hope you come back to play again.")
相关问题