如何使while循环只运行有限次数?

时间:2016-09-11 07:18:26

标签: python

在下面的代码中,我想在roll()之后运行函数deal = input("Your Turn: "),但只能运行有限数量的周期(52)

我怎样才能做到这一点?

import random

ydeal = random.randint(1,15)
adeal = random.randint(1,15)

yscore = 0
ascore = 0

def roll():
    if deal == "Deal":
        print(ydeal, adeal)
        if ydeal > adeal:
            yscore + 1
        elif ydeal < adeal:
            ascore + 1
        print(yscore, ascore)

deal = input("Your Turn: ")
roll()

作为旁注:我注意到在打印yscoreascore时,值在循环期间没有变化,我该如何解决?

5 个答案:

答案 0 :(得分:0)

不确定您的代码,但您可以通过在每个循环上向n添加一个来限制循环次数,并使用while n <= 52(或任何其他值):

n = 1
while n <= 3:
    word = "monkey" if n == 1 else "monkeys"
    print(n, word)
    if input("press Return to increase the number of monkeys ") == "":
        n += 1

print("\nOoops, we ran out of monkeys...")

然后:

1 monkey
press Return to increase the number of monkeys 
2 monkeys
press Return to increase the number of monkeys 
3 monkeys
press Return to increase the number of monkeys 

Ooops, we ran out of monkeys...

在你的情况下:

n = 1
while n <= 52:
    deal = input("Your Turn: ")
    roll()
    n += 1

但是,您的函数roll()不能正常工作:

  • 您的ydealadeal是固定值,在游戏过程中不会更改。
  • 无论你对变量做什么在函数中都没有效果,除非你将变量设置为global,或者让函数返回一些东西。

一个例子:

test = 5

def add_one(var):
    return var + 1

test = add_one(test)
print(test)
> 6

但这是另一个问题:)。

答案 1 :(得分:0)

我注意到您的代码中存在一些问题:

  1. roll()函数内部,您必须声明要使用全局变量而不是函数本地的两个新变量,
  2. 您没有将增量分配给该值。正确的方法是:yscore += 1
  3. 最后,我向您报告您的代码的已审核版本。在代码的最后,我在循环示例中插入了52次迭代。

    import random
    
    ydeal = random.randint(1,9)
    adeal = random.randint(1,9)
    
    yscore = 0
    ascore = 0
    
    def roll():
        #Notify that the variables you want to use are not defined inside the function
        global yscore
        global ascore
        if deal == "!":
            print(ydeal)
            print(adeal)
            if ydeal > adeal:
                yscore += 1
            elif ydeal < adeal:
                ascore += 1
            print(yscore, ascore)
    
    deal = input("Your Turn: ")
    roll()
    
    #while loop
    max_iteration = 52
    counter = 0
    
    while counter < max_iteration :
      counter += 1
      print ("while: ", counter)
    

答案 2 :(得分:0)

我支持Jacob的答案,你可以通过在每个循环上加1来限制循环次数,并在n <= 52(或任何其他值)时使用。我还在http://ubuhanga.com阅读了许多编程技巧,我认为他们也可以帮助你

答案 3 :(得分:0)

根据我的评论

使用range如何做到这一点
import random

def ydeal():
    return random.randint(1,15)
def adeal():
    return random.randint(1,15)

yscore = ascore = draw = 0

def roll():
    global yscore, ascore, draw
    if deal == "Deal":
        for x in range(52):
            yydeal = ydeal()
            aadeal = adeal()
            if yydeal > aadeal:
                yscore += 1
            elif yydeal < aadeal:
                ascore += 1
            else:
                draw += 1
            print("Score:", yscore, ascore, draw)

deal = input("Your Turn: ")
roll()

注意:使用python2.7,您需要将deal = input("Your Turn: ")更改为deal = raw_input("Your Turn: ")

答案 4 :(得分:0)

这是我之前的建议,将while循环放在正确的位置:

import random

yscore = 0
ascore = 0
max_score = 52;

def roll():
    #Notify that the variables you want to use are not defined inside the function
    global yscore
    global ascore
    if deal == "!":
        while (yscore < max_score and ascore < max_score) :
            ydeal = random.randint(1,9)
            adeal = random.randint(1,9)
            print(ydeal)
            print(adeal)
            if ydeal > adeal:
                yscore += 1
            elif ydeal < adeal:
                ascore += 1
            print(yscore, ascore)

deal = input("Your Turn: ")
roll()