【问题标题】:rock paper scissor python program input error石头剪刀布python程序输入错误
【发布时间】:2022-02-13 17:09:09
【问题描述】:

随机导入

def rpc(): a = [“石头”、“纸”、“剪刀”]

b = random.choice(a)

print(b)

userinput = input('please type rock, paper or scissor:')
if userinput == "":
    print("please print the right thing")
    rpc()

if userinput != "rock":
    print("type the right thing!")
    rpc()

if userinput != "paper":
    print("type the right thing!")
    rpc()

if userinput != "scissor":
    print("type the right thing!")
    rpc()


while b == userinput:
    print("it is a draw, do you want to play another one?")
    c = input("type 'y' if you want to play one more time or press 'n': ")
    if c == 'y':
        rpc()
    elif c =='n':
        break
    else:
        print("print the right words please")

if b == 'rock' and userinput == 'paper':
    print("you win!")
    rpc()

elif b == 'rock' and userinput == 'scissor':
    print("you lost")
    rpc()

elif b == 'paper' and userinput == 'scissor':
    print("you win!")
    rpc()

elif b == 'paper' and userinput == 'rock':
    print("you lost!")
    rpc()

elif b == 'scissor' and userinput == 'rock':
    print("you win!")
    rpc()

elif b == 'scissor' and userinput == 'paper':
    print("you lost!")
    rpc()

rpc()

这是我的石头剪刀布代码,它非常简单,但是当我运行我的代码并输入石头剪刀布时,我得到了请打印正确的东西声明,我不知道它为什么会发生,任何帮助太好了,谢谢!

【问题讨论】:

  • 不直接相关:适当地命名您的变量。这可能会花费您几毫秒的打字时间,但它会节省您思考代码的时间。
  • 并将您的代码拆分为逻辑片段。在这里你可以有一个game() 函数、一个ask_move() 函数、一个compute_winer() 函数和一个show_result() 函数。虚构的例子,但你明白了重点:每个功能都应该专注于一个容易推理的事情。
  • Rock-Paper-Scissors Game的可能重复
  • 当您修复所有其他问题时,您会遇到最大深度递归错误:一个了解此网站名称含义的机会

标签: python


【解决方案1】:

让我们清理一下......

userinput = input('please type rock, paper or scissor:')
while userinput not in acceptable_inputs:
    userinput = input('please type rock, paper or scissor:')
opponents_choice = random.choice(objects)
# Check and print. Loop to keep playing - 2 out of 3 wins....

【讨论】:

    【解决方案2】:
    import random
    def winner(riya,siya):
        if (riya==siya):
            return None
        elif (riya=="s"):
            if (siya=="p"):
                return False
            elif(siya=="k"):
                return True
        elif (riya=="p"):
            if (siya=="s"):
                return False
            elif (siya=="k"):
                return True
        elif (riya=="k"):
            if (siya=="s"):
                return True
            elif (siya=="p"):
                return False
            
    print("riya pick: stone(s) paper(p) sessior(k)") 
    a= random.randint(1,3) 
    if a==1:
        riya="s"
    elif a==2:
        riya="p" 
    elif a==3:
        riya="k"
    siya=input(" siya pick: stone(s) paper(p) sessior(k)")
    b=winner(riya,siya)
    print(f"riya pick{riya}")
    print(f"siya pick {siya}")
    if b==None:
        print("tie")
    elif b:
        print("winner")`enter code here`
    else:enter code here
        print("loose")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-10
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多