尝试Rock-Paper-Scissors游戏,但没有工作

时间:2018-04-08 17:29:41

标签: python

所以我是编程的新手,在我学习了这些基础知识后,我试图制作一个典型的Rock-Paper-Scissors游戏,但由于早期脚本中的一些错误,它将无法工作,我在这里找不到解决方案代码......

这是整个代码

import random
this = [1, 2, 3]

try:
    ask = input("Want to Start The Game?(Yes/No) ")
except:
print("Choose something valid!")

else:
    if ask.lower() == 'yes':
    try:
        choose = input("Rock, Paper or Scissors? ")
    except ValueError:
        print("choose something Valid")
    else:
        comp = random.choice(this)
        choose = choose.lower()
        if choose == 'rock':
            choose = int(1)
        elif choose == 'paper':
            choose = int(2)
        else:
            choose = int(3)

        if comp == choose:
            print("Its a draw!")
        elif comp < (choose == 2):
            print("I lose")
        elif comp < (choose == 3):
            print("I win")
        elif (comp == 2) > (choose == 1):
            print ("You lose!")
        elif (comp == 2) < (choose == 3):
            print(":( you win")
        elif (comp == 3) > (choose == 2):
            print("Huh! I win!")
        elif (comp == 3) > (choose == 1):
            print("Good game")
    else:
        quit()

2 个答案:

答案 0 :(得分:-1)

要将输入作为单词输入,例如“是”或“否”,则无法使用input()。这是用于获取整数输入。要将字符串作为输入,您必须使用raw_input()

import random
this = [1, 2, 3]

try:
    ask = raw_input("Want to Start The Game?(Yes/No) ")
except:
  print("Choose something valid!")

else:
    if ask.lower() == 'yes':
      try:
        choose = raw_input("Rock, Paper or Scissors? ")
      except ValueError:
        print("choose something Valid")
      else:
        comp = random.choice(this)
        choose = choose.lower()
        if choose == 'rock':
            choose = int(1)
        elif choose == 'paper':
            choose = int(2)
        else:
            choose = int(3)

        if comp == choose:
            print("Its a draw!")
        elif comp < choose:
            print("I lose")
        elif comp > choose:
            print("I win")
        print("Good game")
    else:
        quit()

答案 1 :(得分:-1)

没有别的缩进问题。 总是照顾不起。 如果没有if语句,你就无法启动else语句。 尝试,除了不是if语句 正在运行的代码=

import random
this = [1, 2, 3]

try:
    ask = input("Want to Start The Game?(Yes/No) ")
except:
    print("Choose something valid!")


if ask.lower() == 'yes':
        try:
            choose = input("Rock, Paper or Scissors? ")
        except ValueError:
            print("choose something Valid")
        else:
            comp = random.choice(this)
            choose = choose.lower()
            if choose == 'rock':
                choose = int(1)
            elif choose == 'paper':
                choose = int(2)
            else:
                choose = int(3)

            if comp == choose:
                print("Its a draw!")
            elif comp < (choose == 2):
                print("I lose")
            elif comp < (choose == 3):
                print("I win")
            elif (comp == 2) > (choose == 1):
                print ("You lose!")
            elif (comp == 2) < (choose == 3):
                print(":( you win")
            elif (comp == 3) > (choose == 2):
                print("Huh! I win!")
            elif (comp == 3) > (choose == 1):
                print("Good game")
else:
        quit()