需要帮助或任何有关PIG骰子游戏的建议

时间:2014-02-17 02:57:34

标签: python

我之前没有编程经验,本学期我正在学习第一个编程课程,需要帮助解决我的错误或建议从哪里开始。我还是要加入计算机对手但是一个我遇到的问题是持有部分有时只能起作用。当它要求再次滚动或按住时我按下H按住它,有时候它会起作用,而其他动作就像我按下R再滚动一样。任何帮助将不胜感激。

这是我到目前为止所做的:

import random

p1 = 0
p2 = 0

print ("Score" "\tYou :", p1, "\tAI :", p2)

still_playing = True
hold_pass_input = False

while still_playing:

    while True:

        p1_turn = input ("Your turn. Hit enter to continue.")
        p1_score = p1
        p2_score = p2
        break

    while (p1_score >= 0 and p1_score <= 50):
        die_roll = random.randint (1,6)

        if die_roll > 1:
            p1_score = die_roll + p1_score
            print("Die :", die_roll, "Pot :", p1_score,)

            hold_pass = input("(R)oll again or (H)old?:").upper()
            while hold_pass_input == False:

                if hold_pass in ["r", "R"]==["r", "R"]:
                    hold_pass_input = True
                    break

                elif hold_pass in["h","H"]==["h","H"]:
                    hold_pass_input = False
                    print ("Score" "\tYou :", p1_score, "\tAI :", p2_score)
                    print("It's the computers turn. Hit enter to continue.")
                    break

                else:
                    hold_pass_input = False
                    print("Use only R, r, H, h")
                    hold_pass = input("(R)oll again or (H)old?:").upper()

        elif die_roll <= 1:
            p1_score = p1_score - p1_score
            print("Die :", die_roll, "Pot :", p1_score, "Bust")
            p2_turn = input("It's the computers turn. Hit enter to continue.")

1 个答案:

答案 0 :(得分:0)

我认为问题出在你的if-elif-else - 阻止。

你这样做:

if hold_pass in ["r", "R"]==["r", "R"]:

关键字in之后的位最有可能评估为True,因此从某种意义上说,该行与以下内容相同:

if hold_pass in True:

这没有多大意义。

尝试这样做:

if hold_pass in ["r", "R"]:

elif - 子句中也存在错误:

elif hold_pass in["h","H"]==["h","H"]:

应该是:

elif hold_pass in ["h","H"]: