Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views6 pages

Jogos Python (Inglês)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Level:1

1)Simulate the game of 2 dice

import random
import time

a=str(input("Name: "))
b=str(input("Name: "))
rounds = int(input("\n Number of rounds: "))
counter = 1
while counter <= rounds:
totalA = 0
totalB = 0
diceA = random.randint(1,6)
diceB = random.randint(1,6)
print("Player {}= {}".format(a,diceA))

print("Player {}= {}".format(b,diceB))

counter += 1
time.sleep(2)
if diceA > diceB:
print("Player {} wins this round!".format(a))
print("="*20,"\n")
totalA += 1
elif diceB > diceA:
print("Player {} wins this round!".format(b))
print("="*20,"\n")
totalB += 1
else:
print("It's a tie!")
print("="*20,"\n")

if totalA > totalB:


print("Player {} is the champion!".format(a))
elif totalB > totalA:
print("Player {} is the champion!".format(b))
else:
print("It's a tie!!")

Step by Step:

1. Import Modules: Import the random and time modules to use functions for generating
random numbers and temporal pauses.

2. User Input: Ask the user to enter the number of rounds they wish to play.
3. Variable Initialization: Define the variables counter, totalA, and totalB to control the
rounds and scores.

4. Rounds Loop: Use a while loop to repeat the game for the desired number of rounds.

5. Random Number Generation: In each round, generate a random number between 1


and 6 for each player, simulating the roll of a die.

6. Results Comparison: Compare the results of the dice and update the scores.

7. Temporal Pause: Use time.sleep(2) to create a 2-second pause between rounds.

8. Determine the Winner: After all rounds, compare the total scores to determine the
winner.
Level:2
2) Rock, paper, scissors

import random
print("=" * 10, "Rock, Paper, Scissors", "=" * 10)
counter = 1
total = 0
while counter != 0:
print("[ 0 ]= Rock\n[ 1 ]= Paper\n[ 2 ]= Scissors")
choice = int(input("Your choice:"))
if choice > 2:
print("Invalid number")
continue
computer = random.randint(0,2)
if computer == 0 and op == 0:
print("It's a tie!!\n")
elif computer == 0 and op == 1:
print("You Win!!!\n")
total=total+1
elif computer == 0 and op ==2:
print("Computer Win!!!\n")
elif computer == 1 and op == 1:
print("It's a tie!!\n")
elif computer == 1 and op == 2:
print("You Win!!!\n")
total=total+1
elif com == 1 and op ==0:
print("Computer Win!!!\n")
elif computer == 2 and op == 2:
print("It's a tie!!\n")
elif computer == 2 and op == 0:
print("You Win!!!\n")
total=total+1
elif computer == 2 and op ==1:
print("Computer Win!!!\n")

decision = str(input("Do you want to continue? [Y/N]")).strip().upper()


if decision == "Y":
continue
elif decision == "N":
counter = 0
else:
print("Invalid. Enter Y or N")
print("You WON {} rounds".format(total))

Step by Step:
1. Import Module: Import the random module to use the random number generation
function.

2. Display Title: Print the game’s title.

3. Game Loop: Use a while loop to allow the game to continue until the user decides to
stop.

4. User Choice: Ask the user to choose between rock, paper, or scissors.

5. Input Validation: Check if the user’s input is valid.

6. Computer Choice: Generate a random choice for the computer.

7. Result Determination: Compare the choices and determine the result of each round.

8. Game Continuation: Ask the user if they wish to continue playing.


Level:3
3) Mega-sena

import random

player_numbers = []

drawn_numbers = []

numberCounter = 1

number_of_numbers = int(input("How many numbers do you want to play? "))

while numberCounter <= number_of_numbers:

number = int(input("Enter the number: "))

numberCounter += 1

player_numbers.append(number)

numberCounter = 1

while numberCounter <= 6:

random_number = random.randint(1, 60)

if random_number not in drawn_numbers:

drawn_numbers.append(random_number)

numberCounter += 1

drawn_numbers.sort()

hits = 0

for number in player_numbers:

if number in drawn_numbers:

hits += 1

print("Drawn numbers:", drawn_numbers)


print("Total numbers hit:", hits)

Step by Step:

1. Import Module: Import the random module to use the random number generation
function.

2. List Initialization: Create two empty lists, player_numbers to store the numbers
chosen by the player, and drawn_numbers for the numbers drawn by the program.

3. User Input: Ask the user to enter the number of numbers they wish to play.

4. Collect User’s Numbers: Use a while loop to collect the numbers chosen by the user
until the desired amount is reached.

5. Generate Drawn Numbers: Generate six random numbers between 1 and 60,
ensuring there are no repetitions.

6. Sort Drawn Numbers: Sort the list of drawn numbers for easier viewing.

7. Identify Hits: Compare the numbers chosen by the player with the drawn numbers
and count the hits.

8. Display Results: Print the drawn numbers and the total number of hits by the player.

You might also like