Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

python - How can I get repeating functions of a script and variable changes

I need to make a game based on the 'drinking' game Ship, Captain, Crew. The game consists of rolling 5 dies, and if the numbers are equal to 6,5,4 (in that order), you win. 6 is the Ship, 5 is the Captain and 4 is the crew. I wrote basic script but I'm still having issues. I have an issue where when I execute the script, the rolls never stop as I'm having issues telling the "roll = random.randint(1,6)" to only run 5 times then stop. Also, 'Dice' Is trying to define the number of rolls. So for example: after the dice has been rolled 5 times, Dice would = 5. I need help making it so that the script recognises that the dice must be rolled 5 times, and when it is, if the numbers rolled were not 6,5 and 4, end the game. Can anyone help?

Code:

import random

Dice = 0
SHIP_CAPTAIN_CREW = 6, 5, 4
SHIP = 6
CAPTAIN = 5
CREW = 4

WINNING_SCORE = 6, 5, 4

while Dice  !=  WINNING_SCORE:
    roll = random.randint(1,6)
   
    
    print ("You Rolled A", roll)

    
        
    if roll == SHIP:
          print("You found the ship!")
     
           
              
          
    if roll == CAPTAIN:
          print("You found the Captain")
               
            
    if roll == CREW:
          print("You found the Crew!")
           
  
        
    if roll == SHIP:
        
        score = +1
        roll
    else:
        Dice    = roll
        
    if roll ==  CAPTAIN :
      
        score = +1
        roll
    else:
        Dice += roll

    
    if Dice == 5:
        break    
print ("Dice:", roll)
question from:https://stackoverflow.com/questions/65893046/how-can-i-get-repeating-functions-of-a-script-and-variable-changes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you want to run it a set number of times, a for loop with a range would be better.

Additionally, the code you have now doesn't check to make sure the numbers are rolled in the correct order.

import random

needed_value = 6; #the next roll you need

for x in range(5) :
    roll = random.randint(1,6)


    print ("You Rolled A", roll)

    
    if roll == 6:
          print("You found the ship!")
          if(needed_value == 6):
                needed_value = 5 #update the next needed number
      
    if roll == 5:
          print("You found the Captain")
          if(needed_value == 5):
               needed_value = 4 #update the next needed number
        
    if roll == 4:
          print("You found the Crew!")
          if(needed_value == 4):
                print ("Win!") #all three were found in order
                break #the game has been won so no need to continue with loop 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...