Python while循环不能像我想要的那样工作。建筑岩石,纸剪刀游戏

时间:2016-12-15 12:53:45

标签: python python-2.7

我的游戏存在很大问题。每当我运行shell(cmd)时,只运行一次,它应该运行10次。我已经尝试了一切,事实上我最近刚刚将while循环更改为一个函数,但它没有帮助。我试过刷新电脑,但这也没有帮助。有人知道如何解决这个问题。

这是我的代码:

import random

pc_rock = 1
pc_paper = 2
pc_scissors = 3

you_rock = ['1', 'rock', 'one']
you_paper = ['2', 'paper', 'two']
you_scissors = ['3', 'scrissors', 'three']

def ifs():
    turns = 0
    while turns < 10:
        turns = turns + 1
        pc_score = 0
        you_score = 0
        if you == you_rock and comp == pc_rock:
            print "Draw"
        elif you == you_rock and comp == pc_paper:
            print "You lose"
            pc_score = pc_score + 1
        elif you == you_rock and comp == pc_scissors:
            print "You win"
            you_score = you_score + 1
        elif you == you_paper and comp == pc_paper:
            print "Draw"
        elif you == you_paper and comp == pc_rock:
            print "You win"
            you_score = you_score + 1
        elif you == you_paper and comp == pc_scissors:
            print "You lose"
            pc_score = pc_score + 1
        elif you == you_scissors and comp == pc_scissors:
            print "Draw"
        elif you == you_scissors and comp == pc_rock:
            print "You lose"
            pc_score = pc_score + 1
        elif you == you_scissors and comp == pc_paper:
            print "You win"
            you_score = you_score + 1
    else:
        print  you_score , "is your score"
        print  pc_score , "is the pc's score"
        if you_score > pc_score:
            print "You win!"
        elif pc_score > you_score:
            print "You lose!"
        elif pc_score == you_score:
            print "Its a Draw!"


comp = random.randrange(1, 4)
print "You are about to play a fun game of rock, paper and scissors against"
print "your pc"
print "It is completely random so the pc can lose"
print "You will play 10 rounds"
print "Do you pick rock, paper or scissors"
print "ROCK = 1"
print "PAPER = 2"
print "SCISSORS = 3"
you = raw_input('>>>>')

#Pc ifs 
#__________________


if comp == pc_rock:
    print "The PC picks rock"
elif comp == pc_paper:
    print "The PC picks paper"
elif comp == pc_scissors:
    print "The PC picks scissors"
else:
    print " ERROR |" * 10
    print "STH WENT WRONG PLEASE GO BACK AND CHECK THE CODE"


#You ifs 
#__________________
if you in you_rock:
    print "You picked rock"
    ifs()
elif you in you_paper:
    print "You picked paper"
    ifs()
elif you in you_scissors:
    print "You picked scissors"
    ifs()
else:
    while you != you_rock or you_paper or you_scissors:
        print "Either rock paper or scissors please!"
        you = raw_input('>>>>')
        ifs()

2 个答案:

答案 0 :(得分:0)

  • 您正在将整数值与列表you == you_rock进行比较,
    在功能内。
  • 因此它返回false,因为while循环中没有其他内容 doen,t打印任何东西。但是循环执行了10次。
  • you == you_rock替换为you == you_rock[0]以及更改 其他如果条件。

现在运行该程序,您将看到它执行了10次。

  

注意:您的while循环错误放置。根据你的改变   要求输出。

答案 1 :(得分:0)

正如其他人所说,你将while循环放在错误的位置。我已经修改了你的代码,以便游戏正常运行10次(并添加了一些print语句来澄清它是什么样的转换),但是仍然需要解决一些错误。

import random

pc_rock = 1
pc_paper = 2
pc_scissors = 3

you_rock = ['1', 'rock', 'one']
you_paper = ['2', 'paper', 'two']
you_scissors = ['3', 'scrissors', 'three']

def game():
    print( "You are about to play a fun game of rock, paper and scissors against")
    print( "your pc")
    print( "It is completely random so the pc can lose")
    print( "You will play 10 rounds")
    turns = 0
    pc_score = 0
    you_score = 0
    while turns < 10:
        comp = random.randrange(1, 4)
        print( "Current round: " + str(turns + 1))
        print( "Your score: " + str(you_score))
        print( "PC score: " + str(pc_score))
        print( "Do you pick rock, paper or scissors")
        print( "ROCK = 1")
        print( "PAPER = 2")
        print( "SCISSORS = 3")
        you = raw_input('>>>>')

        #Pc ifs
        #__________________


        if comp == pc_rock:
            print( "The PC picks rock")
        elif comp == pc_paper:
            print( "The PC picks paper")
        elif comp == pc_scissors:
            print( "The PC picks scissors")
        else:
            print( " ERROR |" * 10)
            print( "STH WENT WRONG PLEASE GO BACK AND CHECK THE CODE")


        #You ifs
        #__________________
        if you in you_rock:
            print( "You picked rock")
        elif you in you_paper:
            print( "You picked paper")
        elif you in you_scissors:
            print( "You picked scissors")
        else:
            while you != you_rock or you_paper or you_scissors:
                print( "Either rock paper or scissors please!")
                you = raw_input('>>>>')
        turns = turns + 1

        if you == you_rock and comp == pc_rock:
            print( "Draw")
        elif you == you_rock and comp == pc_paper:
            print( "You lose")
            pc_score = pc_score + 1
        elif you == you_rock and comp == pc_scissors:
            print( "You win")
            you_score = you_score + 1
        elif you == you_paper and comp == pc_paper:
            print( "Draw")
        elif you == you_paper and comp == pc_rock:
            print( "You win")
            you_score = you_score + 1
        elif you == you_paper and comp == pc_scissors:
            print( "You lose")
            pc_score = pc_score + 1
        elif you == you_scissors and comp == pc_scissors:
            print( "Draw")
        elif you == you_scissors and comp == pc_rock:
            print( "You lose")
            pc_score = pc_score + 1
        elif you == you_scissors and comp == pc_paper:
            print( "You win")
            you_score = you_score + 1
        print('')
    else:
        print(  you_score , "is your score")
        print(  pc_score , "is the pc's score")
        if you_score > pc_score:
            print( "You win!")
        elif pc_score > you_score:
            print( "You lose!")
        elif pc_score == you_score:
            print( "Its a Draw!")

game()

您可能会注意到,ifelif语句的部分用于确定谁赢得了每一轮,并增加了该玩家的得分,但效果不正常。每回合后得分保持在0。我正在考虑修复它,但我认为这将是一个有趣的调试练习。快乐的编码!