Jogos Python (Inglês)
Jogos Python (Inglês)
Jogos Python (Inglês)
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))
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")
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.
6. Results Comparison: Compare the results of the dice and update the scores.
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")
Step by Step:
1. Import Module: Import the random module to use the random number generation
function.
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.
7. Result Determination: Compare the choices and determine the result of each round.
import random
player_numbers = []
drawn_numbers = []
numberCounter = 1
numberCounter += 1
player_numbers.append(number)
numberCounter = 1
drawn_numbers.append(random_number)
numberCounter += 1
drawn_numbers.sort()
hits = 0
if number in drawn_numbers:
hits += 1
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.