简体   繁体   中英

Need help or any advice on a PIG dice game

I have no prior experience with programming and I am taking my first programming class this semester and need some help with what I am doing wrong or advice on where to go from here.I still have to add in the computer opponent but one issue I am having is the hold portion only works sometimes. When it asks to roll again or hold I press H to hold and sometimes it works and others it acts like I hit R for rolling again. Any help will be greatly appreciated.

This is what I have so far:

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.")

I would think the problem lies within your if-elif-else -block.

You do this:

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

The bit after the keyword in would most likely evaluate to True , so in sense the line would be identical to:

if hold_pass in True:

Which doesn't make much sense.

Try doing this instead:

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

The error also exists in the elif -clause:

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

Which should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM