我必须循环特定的代码块 (Python)

I have to loop a specific block of code (Python)

我必须在 Python 中创建一个显示数字 0 - 5 的老虎机,如果玩家获得特定的数字序列,他们将赢得特定数量的钱。我已经创建了所有代码来为玩家评分并打印它,但我无法循环那个块。

我想从 (if wants_play == "p" and player_score >= 0).

开始循环

这是我的代码:

import random

player_score = 1
round_score = 0

wants_play = input("Press 'P' to play or 'Q' to quit. ")
if wants_play == "p" and player_score >= 0:
        print("you have " + str(player_score) + " pound")
        num1 = random.randint(0, 5)
        num2 = random.randint(0, 5)
        num3 = random.randint(0, 5)
        print("you got numbers:", num1, num2, num3)

        round_score = 0
        player_score = 0.2
        if num1 == num2 or num1 == num3 or num2 == num3:
            round_score = 0.5
        if num1 == num2 and num1 == num3 and num2 == num3:
            round_score = 1
        if num1 == 0 and num2 == 0 and num3 == 0:
            round_score = 5
        if num1 == 1 and num2 == 1 or num1 == 1 and num3 ==1 or num2 == 1 and num3 == 1:
            round_score = -1
        if num1 == 1 and num2 == 1 and num3 == 1:
            round_score = 0
            player_score = 0

        print("you won ", round_score, " pounds!")
        player_score += round_score
        if player_score < 0:
            player_score = 0
else:
    print("Goodbye")
    exit()'''

您不能循环,因为您使用了 If 指令而不是循环 (while/for) 如果条件满足,则 if 指令只执行一次,否则,它会进入 else 分支,也执行一次。 如果你想循环,你应该考虑使用 while 指令或 for 指令:

  • while 指令将执行它下面的所有内容,只要满足 while 的条件(为真)。

    同时(wants_play == "p" 和 player_score >= 0): 打印 "hello"

只要 wants_play == "p" and player_score >= 0 就会打印 hello。它不会停止打印 hello 直到其中之一被更改或调用 break 指令。

While( wants_play == "p" and player_score >= 0): 
    print "hello"
    break

只会打印一次 hello,因为调用了 break 关键字,这将终止循环的执行

While( wants_play == "p" and player_score >= 0): 
    print "hello"
    player_score-=1

只要player_score >= 0就会打印hello;每次执行循环,player_score减1,最终会变成<0,也就是while执行结束的时候

因为您使用了关键字,所以这两个条件都必须满足。如果其中之一不再为真,while 的执行将停止

 wants_play == "p" and player_score >= 0

我真的不明白你想用球员得分和回合得分做什么,你想如何实现它们,但我修改了答案所以每次都会问你是否要继续

import random

player_score = 1
round_score = 0

wants_play = input("Press 'P' to play or 'Q' to quit. ")
while(wants_play=="P" or wants_play == "p"):

        print("you have " + str(player_score) + " pound")
        num1 = random.randint(0, 5)
        num2 = random.randint(0, 5)
        num3 = random.randint(0, 5)
        print("you got numbers:", num1, num2, num3)

        round_score = 0
        player_score = 0.2
        if num1 == num2 or num1 == num3 or num2 == num3:
            round_score = 0.5
        if num1 == num2 and num1 == num3 and num2 == num3:
            round_score = 1
        if num1 == 0 and num2 == 0 and num3 == 0:
            round_score = 5
        if num1 == 1 and num2 == 1 or num1 == 1 and num3 ==1 or num2 == 1 and num3 == 1:
            round_score = -1
        if num1 == 1 and num2 == 1 and num3 == 1:
            round_score = 0
            player_score = 0

        print("you won ", round_score, " pounds!")
        player_score += round_score
        if player_score < 0:
            player_score = 0

        wants_play = input("Press 'P' to play or 'Q' to quit. ")


print("Goodbye")
exit()